<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>class CSV - csv: Ruby Standard Library Documentation</title>


<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>

<script src="./js/jquery-3.2.0.min.js"></script>

<script src="./js/vue.min.js"></script>
<script src="./js/js.cookie.min.js"></script>

<link href="./css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="./css/rdoc.css" rel="stylesheet">
<link href="./css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
  var index_rel_prefix = "./";
  var darkModeCsseHref = "./css/rdoc-dm.css"
  var defaultModeCssHref = "./css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="class">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="./index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="./table_of_contents.html#pages">Pages</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#class-CSV-label-CSV">CSV</a>
    <li><a href="#class-CSV-label-In+a+Hurry-3F">In a Hurry?</a>
    <li><a href="#class-CSV-label-CSV+Data">CSV Data</a>
    <li><a href="#class-CSV-label-Class+CSV">Class CSV</a>
    <li><a href="#class-CSV-label-Keeping+It+Simple">Keeping It Simple</a>
    <li><a href="#class-CSV-label-Simple+Parsing">Simple Parsing</a>
    <li><a href="#class-CSV-label-Parsing+a+String">Parsing a String</a>
    <li><a href="#class-CSV-label-Parsing+Via+a+File+Path">Parsing Via a File Path</a>
    <li><a href="#class-CSV-label-Parsing+from+an+Open+IO+Stream">Parsing from an Open IO Stream</a>
    <li><a href="#class-CSV-label-Simple+Generating">Simple Generating</a>
    <li><a href="#class-CSV-label-22Filtering-22+CSV">“Filtering” CSV</a>
    <li><a href="#class-CSV-label-CSV+Objects">CSV Objects</a>
    <li><a href="#class-CSV-label-Instance+Methods">Instance Methods</a>
    <li><a href="#class-CSV-label-Delegated+Methods">Delegated Methods</a>
    <li><a href="#class-CSV-label-Options">Options</a>
    <li><a href="#class-CSV-label-Options+for+Parsing">Options for Parsing</a>
    <li><a href="#class-CSV-label-Option+row_sep">Option <code>row_sep</code></a>
    <li><a href="#class-CSV-label-Option+col_sep">Option <code>col_sep</code></a>
    <li><a href="#class-CSV-label-Option+quote_char">Option <code>quote_char</code></a>
    <li><a href="#class-CSV-label-Option+field_size_limit">Option <code>field_size_limit</code></a>
    <li><a href="#class-CSV-label-Option+converters">Option <code>converters</code></a>
    <li><a href="#class-CSV-label-Option+unconverted_fields">Option <code>unconverted_fields</code></a>
    <li><a href="#class-CSV-label-Option+headers">Option <code>headers</code></a>
    <li><a href="#class-CSV-label-Option+return_headers">Option <code>return_headers</code></a>
    <li><a href="#class-CSV-label-Option+header_converters">Option <code>header_converters</code></a>
    <li><a href="#class-CSV-label-Option+skip_blanks">Option <code>skip_blanks</code></a>
    <li><a href="#class-CSV-label-Option+skip_lines">Option <code>skip_lines</code></a>
    <li><a href="#class-CSV-label-Option+strip">Option <code>strip</code></a>
    <li><a href="#class-CSV-label-Option+liberal_parsing">Option <code>liberal_parsing</code></a>
    <li><a href="#class-CSV-label-Option+nil_value">Option <code>nil_value</code></a>
    <li><a href="#class-CSV-label-Option+empty_value">Option <code>empty_value</code></a>
    <li><a href="#class-CSV-label-Options+for+Generating">Options for Generating</a>
    <li><a href="#class-CSV-label-Option+row_sep">Option <code>row_sep</code></a>
    <li><a href="#class-CSV-label-Option+col_sep">Option <code>col_sep</code></a>
    <li><a href="#class-CSV-label-Option+quote_char">Option <code>quote_char</code></a>
    <li><a href="#class-CSV-label-Option+write_headers">Option <code>write_headers</code></a>
    <li><a href="#class-CSV-label-Option+force_quotes">Option <code>force_quotes</code></a>
    <li><a href="#class-CSV-label-Option+quote_empty">Option <code>quote_empty</code></a>
    <li><a href="#class-CSV-label-Option+write_converters">Option <code>write_converters</code></a>
    <li><a href="#class-CSV-label-Option+write_nil_value">Option <code>write_nil_value</code></a>
    <li><a href="#class-CSV-label-Option+write_empty_value">Option <code>write_empty_value</code></a>
    <li><a href="#class-CSV-label-CSV+with+Headers">CSV with Headers</a>
    <li><a href="#class-CSV-label-Converters">Converters</a>
    <li><a href="#class-CSV-label-Specifying+Converters">Specifying Converters</a>
    <li><a href="#class-CSV-label-Converter+Procs">Converter Procs</a>
    <li><a href="#class-CSV-label-Stored+Converters">Stored Converters</a>
    <li><a href="#class-CSV-label-Converter+Lists">Converter Lists</a>
    <li><a href="#class-CSV-label-Field+Converters">Field Converters</a>
    <li><a href="#class-CSV-label-Built-In+Field+Converters">Built-In Field Converters</a>
    <li><a href="#class-CSV-label-Custom+Field+Converters">Custom Field Converters</a>
    <li><a href="#class-CSV-label-Header+Converters">Header Converters</a>
    <li><a href="#class-CSV-label-Built-In+Header+Converters">Built-In Header Converters</a>
    <li><a href="#class-CSV-label-Custom+Header+Converters">Custom Header Converters</a>
    <li><a href="#class-CSV-label-Write+Converters">Write Converters</a>
    <li><a href="#class-CSV-label-Character+Encodings+-28M17n+or+Multilingualization-29">Character Encodings (M17n or Multilingualization)</a>
  </ul>
</div>


    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
<div id="parent-class-section" class="nav-section">
  <h3>Parent</h3>

  <p class="link"><a href="Object.html">Object</a>
</div>

        
<div id="includes-section" class="nav-section">
  <h3>Included Modules</h3>

  <ul class="link-list">
    <li><span class="include">Enumerable</span>
  </ul>
</div>

        
<div id="extends-section" class="nav-section">
  <h3>Extended With Modules</h3>

  <ul class="link-list">
    <li><span class="extend">Forwardable</span>
  </ul>
</div>

        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-c-filter">::filter</a>
    <li ><a href="#method-c-foreach">::foreach</a>
    <li ><a href="#method-c-generate">::generate</a>
    <li ><a href="#method-c-generate_line">::generate_line</a>
    <li ><a href="#method-c-generate_lines">::generate_lines</a>
    <li ><a href="#method-c-instance">::instance</a>
    <li ><a href="#method-c-new">::new</a>
    <li ><a href="#method-c-open">::open</a>
    <li ><a href="#method-c-parse">::parse</a>
    <li ><a href="#method-c-parse_line">::parse_line</a>
    <li ><a href="#method-c-read">::read</a>
    <li ><a href="#method-c-readlines">::readlines</a>
    <li ><a href="#method-c-table">::table</a>
    <li ><a href="#method-i-3C-3C">#&lt;&lt;</a>
    <li ><a href="#method-i-add_row">#add_row</a>
    <li ><a href="#method-i-binmode-3F">#binmode?</a>
    <li ><a href="#method-i-build_fields_converter">#build_fields_converter</a>
    <li ><a href="#method-i-build_header_fields_converter">#build_header_fields_converter</a>
    <li ><a href="#method-i-build_parser_fields_converter">#build_parser_fields_converter</a>
    <li ><a href="#method-i-build_writer_fields_converter">#build_writer_fields_converter</a>
    <li ><a href="#method-i-col_sep">#col_sep</a>
    <li ><a href="#method-i-convert">#convert</a>
    <li ><a href="#method-i-convert_fields">#convert_fields</a>
    <li ><a href="#method-i-converters">#converters</a>
    <li ><a href="#method-i-determine_encoding">#determine_encoding</a>
    <li ><a href="#method-i-each">#each</a>
    <li ><a href="#method-i-eof">#eof</a>
    <li ><a href="#method-i-eof-3F">#eof?</a>
    <li ><a href="#method-i-field_size_limit">#field_size_limit</a>
    <li ><a href="#method-i-flock">#flock</a>
    <li ><a href="#method-i-force_quotes-3F">#force_quotes?</a>
    <li ><a href="#method-i-gets">#gets</a>
    <li ><a href="#method-i-header_convert">#header_convert</a>
    <li ><a href="#method-i-header_converters">#header_converters</a>
    <li ><a href="#method-i-header_fields_converter">#header_fields_converter</a>
    <li ><a href="#method-i-header_row-3F">#header_row?</a>
    <li ><a href="#method-i-headers">#headers</a>
    <li ><a href="#method-i-inspect">#inspect</a>
    <li ><a href="#method-i-ioctl">#ioctl</a>
    <li ><a href="#method-i-liberal_parsing-3F">#liberal_parsing?</a>
    <li ><a href="#method-i-line">#line</a>
    <li ><a href="#method-i-lineno">#lineno</a>
    <li ><a href="#method-i-max_field_size">#max_field_size</a>
    <li ><a href="#method-i-normalize_converters">#normalize_converters</a>
    <li ><a href="#method-i-parser">#parser</a>
    <li ><a href="#method-i-parser_enumerator">#parser_enumerator</a>
    <li ><a href="#method-i-parser_fields_converter">#parser_fields_converter</a>
    <li ><a href="#method-i-parser_options">#parser_options</a>
    <li ><a href="#method-i-path">#path</a>
    <li ><a href="#method-i-puts">#puts</a>
    <li ><a href="#method-i-quote_char">#quote_char</a>
    <li ><a href="#method-i-raw_encoding">#raw_encoding</a>
    <li ><a href="#method-i-read">#read</a>
    <li ><a href="#method-i-readline">#readline</a>
    <li ><a href="#method-i-readlines">#readlines</a>
    <li ><a href="#method-i-return_headers-3F">#return_headers?</a>
    <li ><a href="#method-i-rewind">#rewind</a>
    <li ><a href="#method-i-row_sep">#row_sep</a>
    <li ><a href="#method-i-shift">#shift</a>
    <li ><a href="#method-i-skip_blanks-3F">#skip_blanks?</a>
    <li ><a href="#method-i-skip_lines">#skip_lines</a>
    <li ><a href="#method-i-stat">#stat</a>
    <li ><a href="#method-i-to_i">#to_i</a>
    <li ><a href="#method-i-to_io">#to_io</a>
    <li ><a href="#method-i-unconverted_fields-3F">#unconverted_fields?</a>
    <li ><a href="#method-i-write_headers-3F">#write_headers?</a>
    <li ><a href="#method-i-writer">#writer</a>
    <li ><a href="#method-i-writer_fields_converter">#writer_fields_converter</a>
    <li ><a href="#method-i-writer_options">#writer_options</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="class-CSV">
    <h1 id="class-CSV" class="class">
      class CSV
    </h1>

    <section class="description">
    
<h2 id="class-CSV-label-CSV">CSV<span><a href="#class-CSV-label-CSV">&para;</a> <a href="#top">&uarr;</a></span></h2>

<h3 id="class-CSV-label-In+a+Hurry-3F">In a Hurry?<span><a href="#class-CSV-label-In+a+Hurry-3F">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>If you are familiar with CSV data and have a particular task in mind, you may want to go directly to the:</p>
<ul><li>
<p><a href="doc/csv/recipes/recipes_rdoc.html">Recipes for CSV</a>.</p>
</li></ul>

<p>Otherwise, read on here, about the API: classes, methods, and constants.</p>

<h3 id="class-CSV-label-CSV+Data">CSV Data<span><a href="#class-CSV-label-CSV+Data">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>CSV (comma-separated values) data is a text representation of a table:</p>
<ul><li>
<p>A <em>row</em> <em>separator</em> delimits table rows. A common row separator is the newline character <code>&quot;\n&quot;</code>.</p>
</li><li>
<p>A <em>column</em> <em>separator</em> delimits fields in a row. A common column separator is the comma character <code>&quot;,&quot;</code>.</p>
</li></ul>

<p>This CSV String, with row separator <code>&quot;\n&quot;</code> and column separator <code>&quot;,&quot;</code>, has three rows and two columns:</p>

<pre class="ruby"><span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
</pre>

<p>Despite the name CSV, a CSV representation can use different separators.</p>

<p>For more about tables, see the Wikipedia article “<a href="https://en.wikipedia.org/wiki/Table_(information)">Table (information)</a>”, especially its section “<a href="https://en.wikipedia.org/wiki/Table_(information)#Simple_table">Simple table</a>”</p>

<h2 id="class-CSV-label-Class+CSV">Class CSV<span><a href="#class-CSV-label-Class+CSV">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Class CSV provides methods for:</p>
<ul><li>
<p>Parsing CSV data from a String object, a File (via its file path), or an IO object.</p>
</li><li>
<p>Generating CSV data to a String object.</p>
</li></ul>

<p>To make CSV available:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;csv&#39;</span>
</pre>

<p>All examples here assume that this has been done.</p>

<h2 id="class-CSV-label-Keeping+It+Simple">Keeping It Simple<span><a href="#class-CSV-label-Keeping+It+Simple">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>A CSV object has dozens of instance methods that offer fine-grained control of parsing and generating CSV data. For many needs, though, simpler approaches will do.</p>

<p>This section summarizes the singleton methods in CSV that allow you to parse and generate without explicitly creating CSV objects. For details, follow the links.</p>

<h3 id="class-CSV-label-Simple+Parsing">Simple Parsing<span><a href="#class-CSV-label-Simple+Parsing">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Parsing methods commonly return either of:</p>
<ul><li>
<p>An Array of Arrays of Strings:</p>
<ul><li>
<p>The outer Array is the entire “table”.</p>
</li><li>
<p>Each inner Array is a row.</p>
</li><li>
<p>Each String is a field.</p>
</li></ul>
</li><li>
<p>A <a href="CSV/Table.html"><code>CSV::Table</code></a> object.  For details, see <a href="#class-CSV-label-CSV+with+Headers">CSV with Headers</a>.</p>
</li></ul>

<h4 id="class-CSV-label-Parsing+a+String">Parsing a String<span><a href="#class-CSV-label-Parsing+a+String">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>The input to be parsed can be a string:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
</pre>

<p>Method <a href="CSV.html#method-c-parse"><code>CSV.parse</code></a> returns the entire CSV data:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>) <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Method <a href="CSV.html#method-c-parse_line"><code>CSV.parse_line</code></a> returns only the first row:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">string</span>) <span class="ruby-comment"># =&gt; [&quot;foo&quot;, &quot;0&quot;]</span>
</pre>

<p>CSV extends class String with instance method String#parse_csv, which also returns only the first row:</p>

<pre class="ruby"><span class="ruby-identifier">string</span>.<span class="ruby-identifier">parse_csv</span> <span class="ruby-comment"># =&gt; [&quot;foo&quot;, &quot;0&quot;]</span>
</pre>

<h4 id="class-CSV-label-Parsing+Via+a+File+Path">Parsing Via a File Path<span><a href="#class-CSV-label-Parsing+Via+a+File+Path">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>The input to be parsed can be in a file:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
</pre>

<p>Method <a href="CSV.html#method-c-read"><code>CSV.read</code></a> returns the entire CSV data:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">path</span>) <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Method <a href="CSV.html#method-c-foreach"><code>CSV.foreach</code></a> iterates, passing each row to the given block:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">foreach</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">row</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-string">&quot;foo&quot;</span>, <span class="ruby-string">&quot;0&quot;</span>]
[<span class="ruby-string">&quot;bar&quot;</span>, <span class="ruby-string">&quot;1&quot;</span>]
[<span class="ruby-string">&quot;baz&quot;</span>, <span class="ruby-string">&quot;2&quot;</span>]
</pre>

<p>Method <a href="CSV.html#method-c-table"><code>CSV.table</code></a> returns the entire CSV data as a <a href="CSV/Table.html"><code>CSV::Table</code></a> object:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">table</span>(<span class="ruby-identifier">path</span>) <span class="ruby-comment"># =&gt; #&lt;CSV::Table mode:col_or_row row_count:3&gt;</span>
</pre>

<h4 id="class-CSV-label-Parsing+from+an+Open+IO+Stream">Parsing from an Open IO Stream<span><a href="#class-CSV-label-Parsing+from+an+Open+IO+Stream">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>The input to be parsed can be in an open IO stream:</p>

<p>Method <a href="CSV.html#method-c-read"><code>CSV.read</code></a> returns the entire CSV data:</p>

<pre class="ruby"><span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">file</span>)
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>As does method <a href="CSV.html#method-c-parse"><code>CSV.parse</code></a>:</p>

<pre class="ruby"><span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">file</span>)
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Method <a href="CSV.html#method-c-parse_line"><code>CSV.parse_line</code></a> returns only the first row:</p>

<pre class="ruby"><span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
 <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">file</span>)
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; [&quot;foo&quot;, &quot;0&quot;]</span>
</pre>

<p>Method <a href="CSV.html#method-c-foreach"><code>CSV.foreach</code></a> iterates, passing each row to the given block:</p>

<pre class="ruby"><span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">foreach</span>(<span class="ruby-identifier">file</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">p</span> <span class="ruby-identifier">row</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-string">&quot;foo&quot;</span>, <span class="ruby-string">&quot;0&quot;</span>]
[<span class="ruby-string">&quot;bar&quot;</span>, <span class="ruby-string">&quot;1&quot;</span>]
[<span class="ruby-string">&quot;baz&quot;</span>, <span class="ruby-string">&quot;2&quot;</span>]
</pre>

<p>Method <a href="CSV.html#method-c-table"><code>CSV.table</code></a> returns the entire CSV data as a <a href="CSV/Table.html"><code>CSV::Table</code></a> object:</p>

<pre class="ruby"><span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">table</span>(<span class="ruby-identifier">file</span>)
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Table mode:col_or_row row_count:3&gt;</span>
</pre>

<h3 id="class-CSV-label-Simple+Generating">Simple Generating<span><a href="#class-CSV-label-Simple+Generating">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Method <a href="CSV.html#method-c-generate"><code>CSV.generate</code></a> returns a String; this example uses method <a href="CSV.html#method-i-3C-3C"><code>CSV#&lt;&lt;</code></a> to append the rows that are to be generated:</p>

<pre class="ruby"><span class="ruby-identifier">output_string</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;bar&#39;</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;baz&#39;</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">output_string</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
</pre>

<p>Method <a href="CSV.html#method-c-generate_line"><code>CSV.generate_line</code></a> returns a String containing the single row constructed from an Array:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_line</span>([<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-string">&#39;0&#39;</span>]) <span class="ruby-comment"># =&gt; &quot;foo,0\n&quot;</span>
</pre>

<p>CSV extends class Array with instance method <code>Array#to_csv</code>, which forms an Array into a String:</p>

<pre class="ruby">[<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-string">&#39;0&#39;</span>].<span class="ruby-identifier">to_csv</span> <span class="ruby-comment"># =&gt; &quot;foo,0\n&quot;</span>
</pre>

<h3 id="class-CSV-label-22Filtering-22+CSV">“Filtering” CSV<span><a href="#class-CSV-label-22Filtering-22+CSV">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Method <a href="CSV.html#method-c-filter"><code>CSV.filter</code></a> provides a Unix-style filter for CSV data. The input data is processed to form the output data:</p>

<pre class="ruby"><span class="ruby-identifier">in_string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">out_string</span> = <span class="ruby-string">&#39;&#39;</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">filter</span>(<span class="ruby-identifier">in_string</span>, <span class="ruby-identifier">out_string</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">row</span>[<span class="ruby-value">0</span>] = <span class="ruby-identifier">row</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">upcase</span>
  <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>] <span class="ruby-operator">*=</span> <span class="ruby-value">4</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">out_string</span> <span class="ruby-comment"># =&gt; &quot;FOO,0000\nBAR,1111\nBAZ,2222\n&quot;</span>
</pre>

<h2 id="class-CSV-label-CSV+Objects">CSV Objects<span><a href="#class-CSV-label-CSV+Objects">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>There are three ways to create a CSV object:</p>
<ul><li>
<p>Method <a href="CSV.html#method-c-new"><code>CSV.new</code></a> returns a new CSV object.</p>
</li><li>
<p>Method <a href="CSV.html#method-c-instance"><code>CSV.instance</code></a> returns a new or cached CSV object.</p>
</li><li>
<p>Method CSV() also returns a new or cached CSV object.</p>
</li></ul>

<h3 id="class-CSV-label-Instance+Methods">Instance Methods<span><a href="#class-CSV-label-Instance+Methods">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>CSV has three groups of instance methods:</p>
<ul><li>
<p>Its own internally defined instance methods.</p>
</li><li>
<p>Methods included by module Enumerable.</p>
</li><li>
<p>Methods delegated to class IO. See below.</p>
</li></ul>

<h4 id="class-CSV-label-Delegated+Methods">Delegated Methods<span><a href="#class-CSV-label-Delegated+Methods">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>For convenience, a <a href="CSV.html"><code>CSV</code></a> object will delegate to many methods in class IO. (A few have wrapper “guard code” in CSV.) You may call:</p>
<ul><li>
<p>IO#binmode</p>
</li><li>
<p><a href="CSV.html#method-i-binmode-3F"><code>binmode?</code></a></p>
</li><li>
<p>IO#close</p>
</li><li>
<p>IO#close_read</p>
</li><li>
<p>IO#close_write</p>
</li><li>
<p>IO#closed?</p>
</li><li>
<p><a href="CSV.html#method-i-eof"><code>eof</code></a></p>
</li><li>
<p><a href="CSV.html#method-i-eof-3F"><code>eof?</code></a></p>
</li><li>
<p>IO#external_encoding</p>
</li><li>
<p>IO#fcntl</p>
</li><li>
<p>IO#fileno</p>
</li><li>
<p><a href="CSV.html#method-i-flock"><code>flock</code></a></p>
</li><li>
<p>IO#flush</p>
</li><li>
<p>IO#fsync</p>
</li><li>
<p>IO#internal_encoding</p>
</li><li>
<p><a href="CSV.html#method-i-ioctl"><code>ioctl</code></a></p>
</li><li>
<p>IO#isatty</p>
</li><li>
<p><a href="CSV.html#method-i-path"><code>path</code></a></p>
</li><li>
<p>IO#pid</p>
</li><li>
<p>IO#pos</p>
</li><li>
<p>IO#pos=</p>
</li><li>
<p>IO#reopen</p>
</li><li>
<p><a href="CSV.html#method-i-rewind"><code>rewind</code></a></p>
</li><li>
<p>IO#seek</p>
</li><li>
<p><a href="CSV.html#method-i-stat"><code>stat</code></a></p>
</li><li>
<p>IO#string</p>
</li><li>
<p>IO#sync</p>
</li><li>
<p>IO#sync=</p>
</li><li>
<p>IO#tell</p>
</li><li>
<p><a href="CSV.html#method-i-to_i"><code>to_i</code></a></p>
</li><li>
<p><a href="CSV.html#method-i-to_io"><code>to_io</code></a></p>
</li><li>
<p>IO#truncate</p>
</li><li>
<p>IO#tty?</p>
</li></ul>

<h3 id="class-CSV-label-Options">Options<span><a href="#class-CSV-label-Options">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>The default values for options are:</p>

<pre class="ruby"><span class="ruby-constant">DEFAULT_OPTIONS</span> = {
  <span class="ruby-comment"># For both parsing and generating.</span>
  <span class="ruby-value">col_sep:</span>            <span class="ruby-string">&quot;,&quot;</span>,
  <span class="ruby-value">row_sep:</span>            <span class="ruby-value">:auto</span>,
  <span class="ruby-value">quote_char:</span>         <span class="ruby-string">&#39;&quot;&#39;</span>,
  <span class="ruby-comment"># For parsing.</span>
  <span class="ruby-value">field_size_limit:</span>   <span class="ruby-keyword">nil</span>,
  <span class="ruby-value">converters:</span>         <span class="ruby-keyword">nil</span>,
  <span class="ruby-value">unconverted_fields:</span> <span class="ruby-keyword">nil</span>,
  <span class="ruby-value">headers:</span>            <span class="ruby-keyword">false</span>,
  <span class="ruby-value">return_headers:</span>     <span class="ruby-keyword">false</span>,
  <span class="ruby-value">header_converters:</span>  <span class="ruby-keyword">nil</span>,
  <span class="ruby-value">skip_blanks:</span>        <span class="ruby-keyword">false</span>,
  <span class="ruby-value">skip_lines:</span>         <span class="ruby-keyword">nil</span>,
  <span class="ruby-value">liberal_parsing:</span>    <span class="ruby-keyword">false</span>,
  <span class="ruby-value">nil_value:</span>          <span class="ruby-keyword">nil</span>,
  <span class="ruby-value">empty_value:</span>        <span class="ruby-string">&quot;&quot;</span>,
  <span class="ruby-value">strip:</span>              <span class="ruby-keyword">false</span>,
  <span class="ruby-comment"># For generating.</span>
  <span class="ruby-value">write_headers:</span>      <span class="ruby-keyword">nil</span>,
  <span class="ruby-value">quote_empty:</span>        <span class="ruby-keyword">true</span>,
  <span class="ruby-value">force_quotes:</span>       <span class="ruby-keyword">false</span>,
  <span class="ruby-value">write_converters:</span>   <span class="ruby-keyword">nil</span>,
  <span class="ruby-value">write_nil_value:</span>    <span class="ruby-keyword">nil</span>,
  <span class="ruby-value">write_empty_value:</span>  <span class="ruby-string">&quot;&quot;</span>,
}
</pre>

<h4 id="class-CSV-label-Options+for+Parsing">Options for Parsing<span><a href="#class-CSV-label-Options+for+Parsing">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>Options for parsing, described in detail below, include:</p>
<ul><li>
<p><code>row_sep</code>: Specifies the row separator; used to delimit rows.</p>
</li><li>
<p><code>col_sep</code>: Specifies the column separator; used to delimit fields.</p>
</li><li>
<p><code>quote_char</code>: Specifies the quote character; used to quote fields.</p>
</li><li>
<p><code>field_size_limit</code>: Specifies the maximum field size + 1 allowed. Deprecated since 3.2.3. Use <code>max_field_size</code> instead.</p>
</li><li>
<p><code>max_field_size</code>: Specifies the maximum field size allowed.</p>
</li><li>
<p><code>converters</code>: Specifies the field converters to be used.</p>
</li><li>
<p><code>unconverted_fields</code>: Specifies whether unconverted fields are to be available.</p>
</li><li>
<p><code>headers</code>: Specifies whether data contains headers, or specifies the headers themselves.</p>
</li><li>
<p><code>return_headers</code>: Specifies whether headers are to be returned.</p>
</li><li>
<p><code>header_converters</code>: Specifies the header converters to be used.</p>
</li><li>
<p><code>skip_blanks</code>: Specifies whether blanks lines are to be ignored.</p>
</li><li>
<p><code>skip_lines</code>: Specifies how comments lines are to be recognized.</p>
</li><li>
<p><code>strip</code>: Specifies whether leading and trailing whitespace are to be stripped from fields. This must be compatible with <code>col_sep</code>; if it is not, then an <code>ArgumentError</code> exception will be raised.</p>
</li><li>
<p><code>liberal_parsing</code>: Specifies whether CSV should attempt to parse non-compliant data.</p>
</li><li>
<p><code>nil_value</code>: Specifies the object that is to be substituted for each null (no-text) field.</p>
</li><li>
<p><code>empty_value</code>: Specifies the object that is to be substituted for each empty field.</p>
</li></ul>

<h6 id="class-CSV-label-Option+row_sep">Option <code>row_sep</code><span><a href="#class-CSV-label-Option+row_sep">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the row separator, a String or the Symbol <code>:auto</code> (see below), to be used for both parsing and generating.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:row_sep</span>) <span class="ruby-comment"># =&gt; :auto</span>
</pre>
<hr>

<p>When <code>row_sep</code> is a String, that String becomes the row separator. The String will be transcoded into the data’s Encoding before use.</p>

<p>Using <code>&quot;\n&quot;</code>:</p>

<pre class="ruby"><span class="ruby-identifier">row_sep</span> = <span class="ruby-string">&quot;\n&quot;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">row_sep:</span> <span class="ruby-identifier">row_sep</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using <code>|</code> (pipe):</p>

<pre class="ruby"><span class="ruby-identifier">row_sep</span> = <span class="ruby-string">&#39;|&#39;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">row_sep:</span> <span class="ruby-identifier">row_sep</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0|bar,1|baz,2|&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">row_sep:</span> <span class="ruby-identifier">row_sep</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using <code>--</code> (two hyphens):</p>

<pre class="ruby"><span class="ruby-identifier">row_sep</span> = <span class="ruby-string">&#39;--&#39;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">row_sep:</span> <span class="ruby-identifier">row_sep</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0--bar,1--baz,2--&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">row_sep:</span> <span class="ruby-identifier">row_sep</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using <code>&#39;&#39;</code> (empty string):</p>

<pre class="ruby"><span class="ruby-identifier">row_sep</span> = <span class="ruby-string">&#39;&#39;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">row_sep:</span> <span class="ruby-identifier">row_sep</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0bar,1baz,2&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">row_sep:</span> <span class="ruby-identifier">row_sep</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0bar&quot;, &quot;1baz&quot;, &quot;2&quot;]]</span>
</pre>
<hr>

<p>When <code>row_sep</code> is the Symbol <code>:auto</code> (the default), generating uses <code>&quot;\n&quot;</code> as the row separator:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
</pre>

<p>Parsing, on the other hand, invokes auto-discovery of the row separator.</p>

<p>Auto-discovery reads ahead in the data looking for the next <code>\r\n</code>, <code>\n</code>, or <code>\r</code> sequence. The sequence will be selected even if it occurs in a quoted field, assuming that you would have the same line endings there.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>The default <code>$INPUT_RECORD_SEPARATOR</code> (<code>$/</code>) is used if any of the following is true:</p>
<ul><li>
<p>None of those sequences is found.</p>
</li><li>
<p>Data is <code>ARGF</code>, <code>STDIN</code>, <code>STDOUT</code>, or <code>STDERR</code>.</p>
</li><li>
<p>The stream is only available for output.</p>
</li></ul>

<p>Obviously, discovery takes a little time. Set manually if speed is important. Also note that IO objects should be opened in binary mode on Windows if this feature will be used as the line-ending translation can cause problems with resetting the document position to where it was before the read ahead.</p>

<h6 id="class-CSV-label-Option+col_sep">Option <code>col_sep</code><span><a href="#class-CSV-label-Option+col_sep">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the String field separator to be used for both parsing and generating. The String will be transcoded into the data’s Encoding before use.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:col_sep</span>) <span class="ruby-comment"># =&gt; &quot;,&quot; (comma)</span>
</pre>

<p>Using the default (comma):</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using <code>:</code> (colon):</p>

<pre class="ruby"><span class="ruby-identifier">col_sep</span> = <span class="ruby-string">&#39;:&#39;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">col_sep:</span> <span class="ruby-identifier">col_sep</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo:0\nbar:1\nbaz:2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">col_sep:</span> <span class="ruby-identifier">col_sep</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using <code>::</code> (two colons):</p>

<pre class="ruby"><span class="ruby-identifier">col_sep</span> = <span class="ruby-string">&#39;::&#39;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">col_sep:</span> <span class="ruby-identifier">col_sep</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo::0\nbar::1\nbaz::2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">col_sep:</span> <span class="ruby-identifier">col_sep</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using <code>&#39;&#39;</code> (empty string):</p>

<pre class="ruby"><span class="ruby-identifier">col_sep</span> = <span class="ruby-string">&#39;&#39;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">col_sep:</span> <span class="ruby-identifier">col_sep</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo0\nbar1\nbaz2\n&quot;</span>
</pre>
<hr>

<p>Raises an exception if parsing with the empty String:</p>

<pre class="ruby"><span class="ruby-identifier">col_sep</span> = <span class="ruby-string">&#39;&#39;</span>
<span class="ruby-comment"># Raises ArgumentError (:col_sep must be 1 or more characters: &quot;&quot;)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&quot;foo0\nbar1\nbaz2\n&quot;</span>, <span class="ruby-value">col_sep:</span> <span class="ruby-identifier">col_sep</span>)
</pre>

<h6 id="class-CSV-label-Option+quote_char">Option <code>quote_char</code><span><a href="#class-CSV-label-Option+quote_char">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the character (String of length 1) used used to quote fields in both parsing and generating. This String will be transcoded into the data’s Encoding before use.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:quote_char</span>) <span class="ruby-comment"># =&gt; &quot;\&quot;&quot; (double quote)</span>
</pre>

<p>This is useful for an application that incorrectly uses <code>&#39;</code> (single-quote) to quote fields, instead of the correct <code>&quot;</code> (double-quote).</p>

<p>Using the default (double quote):</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&quot;&#39;bar&#39;&quot;</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;&quot;baz&quot;&#39;</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0\n&#39;bar&#39;,1\n\&quot;\&quot;\&quot;baz\&quot;\&quot;\&quot;,2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;&#39;bar&#39;&quot;, &quot;1&quot;], [&quot;\&quot;baz\&quot;&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using <code>&#39;</code> (single-quote):</p>

<pre class="ruby"><span class="ruby-identifier">quote_char</span> = <span class="ruby-string">&quot;&#39;&quot;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">quote_char:</span> <span class="ruby-identifier">quote_char</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&quot;&#39;bar&#39;&quot;</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;&quot;baz&quot;&#39;</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0\n&#39;&#39;&#39;bar&#39;&#39;&#39;,1\n\&quot;baz\&quot;,2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">quote_char:</span> <span class="ruby-identifier">quote_char</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;&#39;bar&#39;&quot;, &quot;1&quot;], [&quot;\&quot;baz\&quot;&quot;, &quot;2&quot;]]</span>
</pre>
<hr>

<p>Raises an exception if the String length is greater than 1:</p>

<pre class="ruby"><span class="ruby-comment"># Raises ArgumentError (:quote_char has to be nil or a single character String)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>, <span class="ruby-value">quote_char:</span> <span class="ruby-string">&#39;xx&#39;</span>)
</pre>

<p>Raises an exception if the value is not a String:</p>

<pre class="ruby"><span class="ruby-comment"># Raises ArgumentError (:quote_char has to be nil or a single character String)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>, <span class="ruby-value">quote_char:</span> <span class="ruby-value">:foo</span>)
</pre>

<h6 id="class-CSV-label-Option+field_size_limit">Option <code>field_size_limit</code><span><a href="#class-CSV-label-Option+field_size_limit">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the Integer field size limit.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:field_size_limit</span>) <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>This is a maximum size <a href="CSV.html"><code>CSV</code></a> will read ahead looking for the closing quote for a field. (In truth, it reads to the first line ending beyond this size.) If a quote cannot be found within the limit <a href="CSV.html"><code>CSV</code></a> will raise a <a href="CSV/MalformedCSVError.html"><code>MalformedCSVError</code></a>, assuming the data is faulty. You can use this limit to prevent what are effectively DoS attacks on the parser. However, this limit can cause a legitimate parse to fail; therefore the default value is <code>nil</code> (no limit).</p>

<p>For the examples in this section:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-identifier">&lt;&lt;~EOT</span>
<span class="ruby-value">  &quot;a&quot;,&quot;b&quot;
  &quot;
  2345
  &quot;,&quot;&quot;
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;\&quot;a\&quot;,\&quot;b\&quot;\n\&quot;\n2345\n\&quot;,\&quot;\&quot;\n&quot;</span>
</pre>

<p>Using the default <code>nil</code>:</p>

<pre class="ruby"><span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;a&quot;, &quot;b&quot;], [&quot;\n2345\n&quot;, &quot;&quot;]]</span>
</pre>

<p>Using <code>50</code>:</p>

<pre class="ruby"><span class="ruby-identifier">field_size_limit</span> = <span class="ruby-value">50</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">field_size_limit:</span> <span class="ruby-identifier">field_size_limit</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;a&quot;, &quot;b&quot;], [&quot;\n2345\n&quot;, &quot;&quot;]]</span>
</pre>
<hr>

<p>Raises an exception if a field is too long:</p>

<pre class="ruby"><span class="ruby-identifier">big_str</span> = <span class="ruby-string">&quot;123456789\n&quot;</span> <span class="ruby-operator">*</span> <span class="ruby-value">1024</span>
<span class="ruby-comment"># Raises CSV::MalformedCSVError (Field size exceeded in line 1.)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&#39;valid,fields,&quot;&#39;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">big_str</span> <span class="ruby-operator">+</span> <span class="ruby-string">&#39;&quot;&#39;</span>, <span class="ruby-value">field_size_limit:</span> <span class="ruby-value">2048</span>)
</pre>

<h6 id="class-CSV-label-Option+converters">Option <code>converters</code><span><a href="#class-CSV-label-Option+converters">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies converters to be used in parsing fields. See <a href="#class-CSV-label-Field+Converters">Field Converters</a></p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:converters</span>) <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>The value may be a field converter name (see <a href="#class-CSV-label-Stored+Converters">Stored Converters</a>):</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-string">&#39;1,2,3&#39;</span>
<span class="ruby-comment"># Without a converter</span>
<span class="ruby-identifier">array</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">array</span> <span class="ruby-comment"># =&gt; [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;]</span>
<span class="ruby-comment"># With built-in converter :integer</span>
<span class="ruby-identifier">array</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">converters:</span> <span class="ruby-value">:integer</span>)
<span class="ruby-identifier">array</span> <span class="ruby-comment"># =&gt; [1, 2, 3]</span>
</pre>

<p>The value may be a converter list (see <a href="#class-CSV-label-Converter+Lists">Converter Lists</a>):</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-string">&#39;1,3.14159&#39;</span>
<span class="ruby-comment"># Without converters</span>
<span class="ruby-identifier">array</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">array</span> <span class="ruby-comment"># =&gt; [&quot;1&quot;, &quot;3.14159&quot;]</span>
<span class="ruby-comment"># With built-in converters</span>
<span class="ruby-identifier">array</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">converters:</span> [<span class="ruby-value">:integer</span>, <span class="ruby-value">:float</span>])
<span class="ruby-identifier">array</span> <span class="ruby-comment"># =&gt; [1, 3.14159]</span>
</pre>

<p>The value may be a Proc custom converter: (see <a href="#class-CSV-label-Custom+Field+Converters">Custom Field Converters</a>):</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-string">&#39; foo  ,  bar  ,  baz  &#39;</span>
<span class="ruby-comment"># Without a converter</span>
<span class="ruby-identifier">array</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">array</span> <span class="ruby-comment"># =&gt; [&quot; foo  &quot;, &quot;  bar  &quot;, &quot;  baz  &quot;]</span>
<span class="ruby-comment"># With a custom converter</span>
<span class="ruby-identifier">array</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">converters:</span> <span class="ruby-identifier">proc</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">field</span><span class="ruby-operator">|</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">strip</span> })
<span class="ruby-identifier">array</span> <span class="ruby-comment"># =&gt; [&quot;foo&quot;, &quot;bar&quot;, &quot;baz&quot;]</span>
</pre>

<p>See also <a href="#class-CSV-label-Custom+Field+Converters">Custom Field Converters</a></p>
<hr>

<p>Raises an exception if the converter is not a converter name or a Proc:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-string">&#39;foo,0&#39;</span>
<span class="ruby-comment"># Raises NoMethodError (undefined method `arity&#39; for nil:NilClass)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">converters:</span> <span class="ruby-value">:foo</span>)
</pre>

<h6 id="class-CSV-label-Option+unconverted_fields">Option <code>unconverted_fields</code><span><a href="#class-CSV-label-Option+unconverted_fields">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the boolean that determines whether unconverted field values are to be available.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:unconverted_fields</span>) <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>The unconverted field values are those found in the source data, prior to any conversions performed via option <code>converters</code>.</p>

<p>When option <code>unconverted_fields</code> is <code>true</code>, each returned row (Array or CSV::Row) has an added method, <code>unconverted_fields</code>, that returns the unconverted field values:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-identifier">&lt;&lt;-EOT</span>
<span class="ruby-value">foo,0
bar,1
baz,2
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-comment"># Without unconverted_fields</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">converters:</span> <span class="ruby-value">:integer</span>)
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, 0], [&quot;bar&quot;, 1], [&quot;baz&quot;, 2]]</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:unconverted_fields</span>) <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-comment"># With unconverted_fields</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">converters:</span> <span class="ruby-value">:integer</span>, <span class="ruby-value">unconverted_fields:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, 0], [&quot;bar&quot;, 1], [&quot;baz&quot;, 2]]</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:unconverted_fields</span>) <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">unconverted_fields</span> <span class="ruby-comment"># =&gt; [&quot;foo&quot;, &quot;0&quot;]</span>
</pre>

<h6 id="class-CSV-label-Option+headers">Option <code>headers</code><span><a href="#class-CSV-label-Option+headers">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies a boolean, Symbol, Array, or String to be used to define column headers.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:headers</span>) <span class="ruby-comment"># =&gt; false</span>
</pre>
<hr>

<p>Without <code>headers</code>:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-identifier">&lt;&lt;-EOT</span>
<span class="ruby-value">Name,Count
foo,0
bar,1
bax,2
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; #&lt;CSV io_type:StringIO encoding:UTF-8 lineno:0 col_sep:&quot;,&quot; row_sep:&quot;\n&quot; quote_char:&quot;\&quot;&quot;&gt;</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; nil</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; [&quot;Name&quot;, &quot;Count&quot;]</span>
</pre>
<hr>

<p>If set to <code>true</code> or the Symbol <code>:first_row</code>, the first row of the data is treated as a row of headers:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-identifier">&lt;&lt;-EOT</span>
<span class="ruby-value">Name,Count
foo,0
bar,1
bax,2
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; #&lt;CSV io_type:StringIO encoding:UTF-8 lineno:2 col_sep:&quot;,&quot; row_sep:&quot;\n&quot; quote_char:&quot;\&quot;&quot; headers:[&quot;Name&quot;, &quot;Count&quot;]&gt;</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [&quot;Name&quot;, &quot;Count&quot;]</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Row &quot;Name&quot;:&quot;bar&quot; &quot;Count&quot;:&quot;1&quot;&gt;</span>
</pre>
<hr>

<p>If set to an Array, the Array elements are treated as headers:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-identifier">&lt;&lt;-EOT</span>
<span class="ruby-value">foo,0
bar,1
bax,2
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">headers:</span> [<span class="ruby-string">&#39;Name&#39;</span>, <span class="ruby-string">&#39;Count&#39;</span>])
<span class="ruby-identifier">csv</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [&quot;Name&quot;, &quot;Count&quot;]</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Row &quot;Name&quot;:&quot;bar&quot; &quot;Count&quot;:&quot;1&quot;&gt;</span>
</pre>
<hr>

<p>If set to a String <code>str</code>, method <code>CSV::parse_line(str, options)</code> is called with the current <code>options</code>, and the returned Array is treated as headers:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-identifier">&lt;&lt;-EOT</span>
<span class="ruby-value">foo,0
bar,1
bax,2
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">headers:</span> <span class="ruby-string">&#39;Name,Count&#39;</span>)
<span class="ruby-identifier">csv</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [&quot;Name&quot;, &quot;Count&quot;]</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Row &quot;Name&quot;:&quot;bar&quot; &quot;Count&quot;:&quot;1&quot;&gt;</span>
</pre>

<h6 id="class-CSV-label-Option+return_headers">Option <code>return_headers</code><span><a href="#class-CSV-label-Option+return_headers">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the boolean that determines whether method <a href="CSV.html#method-i-shift"><code>shift</code></a> returns or ignores the header row.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:return_headers</span>) <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>Examples:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-identifier">&lt;&lt;-EOT</span>
<span class="ruby-value">Name,Count
foo,0
bar,1
bax,2
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-comment"># Without return_headers first row is str.</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Row &quot;Name&quot;:&quot;foo&quot; &quot;Count&quot;:&quot;0&quot;&gt;</span>
<span class="ruby-comment"># With return_headers first row is headers.</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>, <span class="ruby-value">return_headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Row &quot;Name&quot;:&quot;Name&quot; &quot;Count&quot;:&quot;Count&quot;&gt;</span>
</pre>

<h6 id="class-CSV-label-Option+header_converters">Option <code>header_converters</code><span><a href="#class-CSV-label-Option+header_converters">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies converters to be used in parsing headers. See <a href="#class-CSV-label-Header+Converters">Header Converters</a></p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:header_converters</span>) <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>Identical in functionality to option <a href="#class-CSV-label-Option+converters">converters</a> except that:</p>
<ul><li>
<p>The converters apply only to the header row.</p>
</li><li>
<p>The built-in header converters are <code>:downcase</code> and <code>:symbol</code>.</p>
</li></ul>

<p>This section assumes prior execution of:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-identifier">&lt;&lt;-EOT</span>
<span class="ruby-value">Name,Value
foo,0
bar,1
baz,2
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-comment"># With no header converter</span>
<span class="ruby-identifier">table</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">table</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [&quot;Name&quot;, &quot;Value&quot;]</span>
</pre>

<p>The value may be a header converter name (see <a href="#class-CSV-label-Stored+Converters">Stored Converters</a>):</p>

<pre class="ruby"><span class="ruby-identifier">table</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>, <span class="ruby-value">header_converters:</span> <span class="ruby-value">:downcase</span>)
<span class="ruby-identifier">table</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [&quot;name&quot;, &quot;value&quot;]</span>
</pre>

<p>The value may be a converter list (see <a href="#class-CSV-label-Converter+Lists">Converter Lists</a>):</p>

<pre class="ruby"><span class="ruby-identifier">header_converters</span> = [<span class="ruby-value">:downcase</span>, <span class="ruby-value">:symbol</span>]
<span class="ruby-identifier">table</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>, <span class="ruby-value">header_converters:</span> <span class="ruby-identifier">header_converters</span>)
<span class="ruby-identifier">table</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [:name, :value]</span>
</pre>

<p>The value may be a Proc custom converter (see <a href="#class-CSV-label-Custom+Header+Converters">Custom Header Converters</a>):</p>

<pre class="ruby"><span class="ruby-identifier">upcase_converter</span> = <span class="ruby-identifier">proc</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">field</span><span class="ruby-operator">|</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">upcase</span> }
<span class="ruby-identifier">table</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>, <span class="ruby-value">header_converters:</span> <span class="ruby-identifier">upcase_converter</span>)
<span class="ruby-identifier">table</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [&quot;NAME&quot;, &quot;VALUE&quot;]</span>
</pre>

<p>See also <a href="#class-CSV-label-Custom+Header+Converters">Custom Header Converters</a></p>

<h6 id="class-CSV-label-Option+skip_blanks">Option <code>skip_blanks</code><span><a href="#class-CSV-label-Option+skip_blanks">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies a boolean that determines whether blank lines in the input will be ignored; a line that contains a column separator is not considered to be blank.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:skip_blanks</span>) <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>See also option <a href="#class-CSV-label-Option+skip_lines">skiplines</a>.</p>

<p>For examples in this section:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-identifier">&lt;&lt;-EOT</span>
<span class="ruby-value">foo,0

bar,1
baz,2

,
</span><span class="ruby-identifier">EOT</span>
</pre>

<p>Using the default, <code>false</code>:</p>

<pre class="ruby"><span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;], [], [nil, nil]]</span>
</pre>

<p>Using <code>true</code>:</p>

<pre class="ruby"><span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">skip_blanks:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;], [nil, nil]]</span>
</pre>

<p>Using a truthy value:</p>

<pre class="ruby"><span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">skip_blanks:</span> <span class="ruby-value">:foo</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;], [nil, nil]]</span>
</pre>

<h6 id="class-CSV-label-Option+skip_lines">Option <code>skip_lines</code><span><a href="#class-CSV-label-Option+skip_lines">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies an object to use in identifying comment lines in the input that are to be ignored:</p>
<ul><li>
<p>If a Regexp, ignores lines that match it.</p>
</li><li>
<p>If a String, converts it to a Regexp, ignores lines that match it.</p>
</li><li>
<p>If <code>nil</code>, no lines are considered to be comments.</p>
</li></ul>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:skip_lines</span>) <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>For examples in this section:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-identifier">&lt;&lt;-EOT</span>
<span class="ruby-value"># Comment
foo,0
bar,1
baz,2
# Another comment
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;# Comment\nfoo,0\nbar,1\nbaz,2\n# Another comment\n&quot;</span>
</pre>

<p>Using the default, <code>nil</code>:</p>

<pre class="ruby"><span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;# Comment&quot;], [&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;], [&quot;# Another comment&quot;]]</span>
</pre>

<p>Using a Regexp:</p>

<pre class="ruby"><span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">skip_lines:</span> <span class="ruby-regexp">/^#/</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using a String:</p>

<pre class="ruby"><span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">skip_lines:</span> <span class="ruby-string">&#39;#&#39;</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>
<hr>

<p>Raises an exception if given an object that is not a Regexp, a String, or <code>nil</code>:</p>

<pre class="ruby"><span class="ruby-comment"># Raises ArgumentError (:skip_lines has to respond to #match: 0)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">skip_lines:</span> <span class="ruby-value">0</span>)
</pre>

<h6 id="class-CSV-label-Option+strip">Option <code>strip</code><span><a href="#class-CSV-label-Option+strip">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the boolean value that determines whether whitespace is stripped from each input field.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:strip</span>) <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>With default value <code>false</code>:</p>

<pre class="ruby"><span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-string">&#39; a , b &#39;</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [&quot; a &quot;, &quot; b &quot;]</span>
</pre>

<p>With value <code>true</code>:</p>

<pre class="ruby"><span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-string">&#39; a , b &#39;</span>, <span class="ruby-value">strip:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;b&quot;]</span>
</pre>

<h6 id="class-CSV-label-Option+liberal_parsing">Option <code>liberal_parsing</code><span><a href="#class-CSV-label-Option+liberal_parsing">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the boolean or hash value that determines whether <a href="CSV.html"><code>CSV</code></a> will attempt to parse input not conformant with RFC 4180, such as double quotes in unquoted fields.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:liberal_parsing</span>) <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>For the next two examples:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-string">&#39;is,this &quot;three, or four&quot;,fields&#39;</span>
</pre>

<p>Without <code>liberal_parsing</code>:</p>

<pre class="ruby"><span class="ruby-comment"># Raises CSV::MalformedCSVError (Illegal quoting in str 1.)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">str</span>)
</pre>

<p>With <code>liberal_parsing</code>:</p>

<pre class="ruby"><span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">liberal_parsing:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [&quot;is&quot;, &quot;this \&quot;three&quot;, &quot; or four\&quot;&quot;, &quot;fields&quot;]</span>
</pre>

<p>Use the <code>backslash_quote</code> sub-option to parse values that use a backslash to escape a double-quote character.  This causes the parser to treat <code>\&quot;</code> as if it were <code>&quot;&quot;</code>.</p>

<p>For the next two examples:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-string">&#39;Show,&quot;Harry \&quot;Handcuff\&quot; Houdini, the one and only&quot;,&quot;Tampa Theater&quot;&#39;</span>
</pre>

<p>With <code>liberal_parsing</code>, but without the <code>backslash_quote</code> sub-option:</p>

<pre class="ruby"><span class="ruby-comment"># Incorrect interpretation of backslash; incorrectly interprets the quoted comma as a field separator.</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">liberal_parsing:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [&quot;Show&quot;, &quot;\&quot;Harry \\\&quot;Handcuff\\\&quot; Houdini&quot;, &quot; the one and only\&quot;&quot;, &quot;Tampa Theater&quot;]</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">ary</span>[<span class="ruby-value">1</span>] <span class="ruby-comment"># =&gt; &quot;Harry \&quot;Handcuff\&quot; Houdini</span>
</pre>

<p>With <code>liberal_parsing</code> and its <code>backslash_quote</code> sub-option:</p>

<pre class="ruby"><span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">liberal_parsing:</span> { <span class="ruby-value">backslash_quote:</span> <span class="ruby-keyword">true</span> })
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [&quot;Show&quot;, &quot;Harry \&quot;Handcuff\&quot; Houdini, the one and only&quot;, &quot;Tampa Theater&quot;]</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">ary</span>[<span class="ruby-value">1</span>] <span class="ruby-comment"># =&gt; Harry &quot;Handcuff&quot; Houdini, the one and only</span>
</pre>

<h6 id="class-CSV-label-Option+nil_value">Option <code>nil_value</code><span><a href="#class-CSV-label-Option+nil_value">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the object that is to be substituted for each null (no-text) field.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:nil_value</span>) <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>With the default, <code>nil</code>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-string">&#39;a,,b,,c&#39;</span>) <span class="ruby-comment"># =&gt; [&quot;a&quot;, nil, &quot;b&quot;, nil, &quot;c&quot;]</span>
</pre>

<p>With a different object:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-string">&#39;a,,b,,c&#39;</span>, <span class="ruby-value">nil_value:</span> <span class="ruby-value">0</span>) <span class="ruby-comment"># =&gt; [&quot;a&quot;, 0, &quot;b&quot;, 0, &quot;c&quot;]</span>
</pre>

<h6 id="class-CSV-label-Option+empty_value">Option <code>empty_value</code><span><a href="#class-CSV-label-Option+empty_value">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the object that is to be substituted for each field that has an empty String.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:empty_value</span>) <span class="ruby-comment"># =&gt; &quot;&quot; (empty string)</span>
</pre>

<p>With the default, <code>&quot;&quot;</code>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-string">&#39;a,&quot;&quot;,b,&quot;&quot;,c&#39;</span>) <span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;&quot;, &quot;b&quot;, &quot;&quot;, &quot;c&quot;]</span>
</pre>

<p>With a different object:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-string">&#39;a,&quot;&quot;,b,&quot;&quot;,c&#39;</span>, <span class="ruby-value">empty_value:</span> <span class="ruby-string">&#39;x&#39;</span>) <span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;x&quot;, &quot;b&quot;, &quot;x&quot;, &quot;c&quot;]</span>
</pre>

<h4 id="class-CSV-label-Options+for+Generating">Options for Generating<span><a href="#class-CSV-label-Options+for+Generating">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>Options for generating, described in detail below, include:</p>
<ul><li>
<p><code>row_sep</code>: Specifies the row separator; used to delimit rows.</p>
</li><li>
<p><code>col_sep</code>: Specifies the column separator; used to delimit fields.</p>
</li><li>
<p><code>quote_char</code>: Specifies the quote character; used to quote fields.</p>
</li><li>
<p><code>write_headers</code>: Specifies whether headers are to be written.</p>
</li><li>
<p><code>force_quotes</code>: Specifies whether each output field is to be quoted.</p>
</li><li>
<p><code>quote_empty</code>: Specifies whether each empty output field is to be quoted.</p>
</li><li>
<p><code>write_converters</code>: Specifies the field converters to be used in writing.</p>
</li><li>
<p><code>write_nil_value</code>: Specifies the object that is to be substituted for each <code>nil</code>-valued field.</p>
</li><li>
<p><code>write_empty_value</code>: Specifies the object that is to be substituted for each empty field.</p>
</li></ul>

<h6 id="class-CSV-label-Option+row_sep">Option <code>row_sep</code><span><a href="#class-CSV-label-Option+row_sep">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the row separator, a String or the Symbol <code>:auto</code> (see below), to be used for both parsing and generating.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:row_sep</span>) <span class="ruby-comment"># =&gt; :auto</span>
</pre>
<hr>

<p>When <code>row_sep</code> is a String, that String becomes the row separator. The String will be transcoded into the data’s Encoding before use.</p>

<p>Using <code>&quot;\n&quot;</code>:</p>

<pre class="ruby"><span class="ruby-identifier">row_sep</span> = <span class="ruby-string">&quot;\n&quot;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">row_sep:</span> <span class="ruby-identifier">row_sep</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using <code>|</code> (pipe):</p>

<pre class="ruby"><span class="ruby-identifier">row_sep</span> = <span class="ruby-string">&#39;|&#39;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">row_sep:</span> <span class="ruby-identifier">row_sep</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0|bar,1|baz,2|&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">row_sep:</span> <span class="ruby-identifier">row_sep</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using <code>--</code> (two hyphens):</p>

<pre class="ruby"><span class="ruby-identifier">row_sep</span> = <span class="ruby-string">&#39;--&#39;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">row_sep:</span> <span class="ruby-identifier">row_sep</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0--bar,1--baz,2--&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">row_sep:</span> <span class="ruby-identifier">row_sep</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using <code>&#39;&#39;</code> (empty string):</p>

<pre class="ruby"><span class="ruby-identifier">row_sep</span> = <span class="ruby-string">&#39;&#39;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">row_sep:</span> <span class="ruby-identifier">row_sep</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0bar,1baz,2&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">row_sep:</span> <span class="ruby-identifier">row_sep</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0bar&quot;, &quot;1baz&quot;, &quot;2&quot;]]</span>
</pre>
<hr>

<p>When <code>row_sep</code> is the Symbol <code>:auto</code> (the default), generating uses <code>&quot;\n&quot;</code> as the row separator:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
</pre>

<p>Parsing, on the other hand, invokes auto-discovery of the row separator.</p>

<p>Auto-discovery reads ahead in the data looking for the next <code>\r\n</code>, <code>\n</code>, or <code>\r</code> sequence. The sequence will be selected even if it occurs in a quoted field, assuming that you would have the same line endings there.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>The default <code>$INPUT_RECORD_SEPARATOR</code> (<code>$/</code>) is used if any of the following is true:</p>
<ul><li>
<p>None of those sequences is found.</p>
</li><li>
<p>Data is <code>ARGF</code>, <code>STDIN</code>, <code>STDOUT</code>, or <code>STDERR</code>.</p>
</li><li>
<p>The stream is only available for output.</p>
</li></ul>

<p>Obviously, discovery takes a little time. Set manually if speed is important. Also note that IO objects should be opened in binary mode on Windows if this feature will be used as the line-ending translation can cause problems with resetting the document position to where it was before the read ahead.</p>

<h6 id="class-CSV-label-Option+col_sep">Option <code>col_sep</code><span><a href="#class-CSV-label-Option+col_sep">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the String field separator to be used for both parsing and generating. The String will be transcoded into the data’s Encoding before use.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:col_sep</span>) <span class="ruby-comment"># =&gt; &quot;,&quot; (comma)</span>
</pre>

<p>Using the default (comma):</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using <code>:</code> (colon):</p>

<pre class="ruby"><span class="ruby-identifier">col_sep</span> = <span class="ruby-string">&#39;:&#39;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">col_sep:</span> <span class="ruby-identifier">col_sep</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo:0\nbar:1\nbaz:2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">col_sep:</span> <span class="ruby-identifier">col_sep</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using <code>::</code> (two colons):</p>

<pre class="ruby"><span class="ruby-identifier">col_sep</span> = <span class="ruby-string">&#39;::&#39;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">col_sep:</span> <span class="ruby-identifier">col_sep</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo::0\nbar::1\nbaz::2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">col_sep:</span> <span class="ruby-identifier">col_sep</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using <code>&#39;&#39;</code> (empty string):</p>

<pre class="ruby"><span class="ruby-identifier">col_sep</span> = <span class="ruby-string">&#39;&#39;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">col_sep:</span> <span class="ruby-identifier">col_sep</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo0\nbar1\nbaz2\n&quot;</span>
</pre>
<hr>

<p>Raises an exception if parsing with the empty String:</p>

<pre class="ruby"><span class="ruby-identifier">col_sep</span> = <span class="ruby-string">&#39;&#39;</span>
<span class="ruby-comment"># Raises ArgumentError (:col_sep must be 1 or more characters: &quot;&quot;)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&quot;foo0\nbar1\nbaz2\n&quot;</span>, <span class="ruby-value">col_sep:</span> <span class="ruby-identifier">col_sep</span>)
</pre>

<h6 id="class-CSV-label-Option+quote_char">Option <code>quote_char</code><span><a href="#class-CSV-label-Option+quote_char">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the character (String of length 1) used used to quote fields in both parsing and generating. This String will be transcoded into the data’s Encoding before use.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:quote_char</span>) <span class="ruby-comment"># =&gt; &quot;\&quot;&quot; (double quote)</span>
</pre>

<p>This is useful for an application that incorrectly uses <code>&#39;</code> (single-quote) to quote fields, instead of the correct <code>&quot;</code> (double-quote).</p>

<p>Using the default (double quote):</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&quot;&#39;bar&#39;&quot;</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;&quot;baz&quot;&#39;</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0\n&#39;bar&#39;,1\n\&quot;\&quot;\&quot;baz\&quot;\&quot;\&quot;,2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;&#39;bar&#39;&quot;, &quot;1&quot;], [&quot;\&quot;baz\&quot;&quot;, &quot;2&quot;]]</span>
</pre>

<p>Using <code>&#39;</code> (single-quote):</p>

<pre class="ruby"><span class="ruby-identifier">quote_char</span> = <span class="ruby-string">&quot;&#39;&quot;</span>
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">quote_char:</span> <span class="ruby-identifier">quote_char</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&quot;&#39;bar&#39;&quot;</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;&quot;baz&quot;&#39;</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0\n&#39;&#39;&#39;bar&#39;&#39;&#39;,1\n\&quot;baz\&quot;,2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-value">quote_char:</span> <span class="ruby-identifier">quote_char</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;&#39;bar&#39;&quot;, &quot;1&quot;], [&quot;\&quot;baz\&quot;&quot;, &quot;2&quot;]]</span>
</pre>
<hr>

<p>Raises an exception if the String length is greater than 1:</p>

<pre class="ruby"><span class="ruby-comment"># Raises ArgumentError (:quote_char has to be nil or a single character String)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>, <span class="ruby-value">quote_char:</span> <span class="ruby-string">&#39;xx&#39;</span>)
</pre>

<p>Raises an exception if the value is not a String:</p>

<pre class="ruby"><span class="ruby-comment"># Raises ArgumentError (:quote_char has to be nil or a single character String)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>, <span class="ruby-value">quote_char:</span> <span class="ruby-value">:foo</span>)
</pre>

<h6 id="class-CSV-label-Option+write_headers">Option <code>write_headers</code><span><a href="#class-CSV-label-Option+write_headers">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the boolean that determines whether a header row is included in the output; ignored if there are no headers.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:write_headers</span>) <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>Without <code>write_headers</code>:</p>

<pre class="ruby"><span class="ruby-identifier">file_path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">file_path</span>,<span class="ruby-string">&#39;w&#39;</span>,
    <span class="ruby-value">:headers</span> <span class="ruby-operator">=&gt;</span> [<span class="ruby-string">&#39;Name&#39;</span>,<span class="ruby-string">&#39;Value&#39;</span>]
  ) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-string">&#39;0&#39;</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">file_path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; [&quot;foo&quot;, &quot;0&quot;]</span>
</pre>

<p>With <code>write_headers</code>“:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">file_path</span>,<span class="ruby-string">&#39;w&#39;</span>,
    <span class="ruby-value">:write_headers</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-keyword">true</span>,
    <span class="ruby-value">:headers</span> <span class="ruby-operator">=&gt;</span> [<span class="ruby-string">&#39;Name&#39;</span>,<span class="ruby-string">&#39;Value&#39;</span>]
  ) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-string">&#39;0&#39;</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">file_path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; [&quot;Name&quot;, &quot;Value&quot;]</span>
</pre>

<h6 id="class-CSV-label-Option+force_quotes">Option <code>force_quotes</code><span><a href="#class-CSV-label-Option+force_quotes">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the boolean that determines whether each output field is to be double-quoted.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:force_quotes</span>) <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>For examples in this section:</p>

<pre class="ruby"><span class="ruby-identifier">ary</span> = [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-value">0</span>, <span class="ruby-keyword">nil</span>]
</pre>

<p>Using the default, <code>false</code>:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_line</span>(<span class="ruby-identifier">ary</span>)
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;foo,0,\n&quot;</span>
</pre>

<p>Using <code>true</code>:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_line</span>(<span class="ruby-identifier">ary</span>, <span class="ruby-value">force_quotes:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;\&quot;foo\&quot;,\&quot;0\&quot;,\&quot;\&quot;\n&quot;</span>
</pre>

<h6 id="class-CSV-label-Option+quote_empty">Option <code>quote_empty</code><span><a href="#class-CSV-label-Option+quote_empty">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the boolean that determines whether an empty value is to be double-quoted.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:quote_empty</span>) <span class="ruby-comment"># =&gt; true</span>
</pre>

<p>With the default <code>true</code>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_line</span>([<span class="ruby-string">&#39;&quot;&#39;</span>, <span class="ruby-string">&quot;&quot;</span>]) <span class="ruby-comment"># =&gt; &quot;\&quot;\&quot;\&quot;\&quot;,\&quot;\&quot;\n&quot;</span>
</pre>

<p>With <code>false</code>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_line</span>([<span class="ruby-string">&#39;&quot;&#39;</span>, <span class="ruby-string">&quot;&quot;</span>], <span class="ruby-value">quote_empty:</span> <span class="ruby-keyword">false</span>) <span class="ruby-comment"># =&gt; &quot;\&quot;\&quot;\&quot;\&quot;,\n&quot;</span>
</pre>

<h6 id="class-CSV-label-Option+write_converters">Option <code>write_converters</code><span><a href="#class-CSV-label-Option+write_converters">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies converters to be used in generating fields. See <a href="#class-CSV-label-Write+Converters">Write Converters</a></p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:write_converters</span>) <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>With no write converter:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_line</span>([<span class="ruby-string">&quot;\na\n&quot;</span>, <span class="ruby-string">&quot;\tb\t&quot;</span>, <span class="ruby-string">&quot; c &quot;</span>])
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;\&quot;\na\n\&quot;,\tb\t, c \n&quot;</span>
</pre>

<p>With a write converter:</p>

<pre class="ruby"><span class="ruby-identifier">strip_converter</span> = <span class="ruby-identifier">proc</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">field</span><span class="ruby-operator">|</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">strip</span> }
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_line</span>([<span class="ruby-string">&quot;\na\n&quot;</span>, <span class="ruby-string">&quot;\tb\t&quot;</span>, <span class="ruby-string">&quot; c &quot;</span>], <span class="ruby-value">write_converters:</span> <span class="ruby-identifier">strip_converter</span>)
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;a,b,c\n&quot;</span>
</pre>

<p>With two write converters (called in order):</p>

<pre class="ruby"><span class="ruby-identifier">upcase_converter</span> = <span class="ruby-identifier">proc</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">field</span><span class="ruby-operator">|</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">upcase</span> }
<span class="ruby-identifier">downcase_converter</span> = <span class="ruby-identifier">proc</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">field</span><span class="ruby-operator">|</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">downcase</span> }
<span class="ruby-identifier">write_converters</span> = [<span class="ruby-identifier">upcase_converter</span>, <span class="ruby-identifier">downcase_converter</span>]
<span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_line</span>([<span class="ruby-string">&#39;a&#39;</span>, <span class="ruby-string">&#39;b&#39;</span>, <span class="ruby-string">&#39;c&#39;</span>], <span class="ruby-value">write_converters:</span> <span class="ruby-identifier">write_converters</span>)
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;a,b,c\n&quot;</span>
</pre>

<p>See also <a href="#class-CSV-label-Write+Converters">Write Converters</a></p>

<h6 id="class-CSV-label-Option+write_nil_value">Option <code>write_nil_value</code><span><a href="#class-CSV-label-Option+write_nil_value">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the object that is to be substituted for each <code>nil</code>-valued field.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:write_nil_value</span>) <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>Without the option:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_line</span>([<span class="ruby-string">&#39;a&#39;</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-string">&#39;c&#39;</span>, <span class="ruby-keyword">nil</span>])
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;a,,c,\n&quot;</span>
</pre>

<p>With the option:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_line</span>([<span class="ruby-string">&#39;a&#39;</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-string">&#39;c&#39;</span>, <span class="ruby-keyword">nil</span>], <span class="ruby-value">write_nil_value:</span> <span class="ruby-string">&quot;x&quot;</span>)
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;a,x,c,x\n&quot;</span>
</pre>

<h6 id="class-CSV-label-Option+write_empty_value">Option <code>write_empty_value</code><span><a href="#class-CSV-label-Option+write_empty_value">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Specifies the object that is to be substituted for each field that has an empty String.</p>

<p>Default value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:write_empty_value</span>) <span class="ruby-comment"># =&gt; &quot;&quot;</span>
</pre>

<p>Without the option:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_line</span>([<span class="ruby-string">&#39;a&#39;</span>, <span class="ruby-string">&#39;&#39;</span>, <span class="ruby-string">&#39;c&#39;</span>, <span class="ruby-string">&#39;&#39;</span>])
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;a,\&quot;\&quot;,c,\&quot;\&quot;\n&quot;</span>
</pre>

<p>With the option:</p>

<pre class="ruby"><span class="ruby-identifier">str</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_line</span>([<span class="ruby-string">&#39;a&#39;</span>, <span class="ruby-string">&#39;&#39;</span>, <span class="ruby-string">&#39;c&#39;</span>, <span class="ruby-string">&#39;&#39;</span>], <span class="ruby-value">write_empty_value:</span> <span class="ruby-string">&quot;x&quot;</span>)
<span class="ruby-identifier">str</span> <span class="ruby-comment"># =&gt; &quot;a,x,c,x\n&quot;</span>
</pre>

<h3 id="class-CSV-label-CSV+with+Headers">CSV with Headers<span><a href="#class-CSV-label-CSV+with+Headers">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p><a href="CSV.html"><code>CSV</code></a> allows to specify column names of <a href="CSV.html"><code>CSV</code></a> file, whether they are in data, or provided separately. If headers are specified, reading methods return an instance of <a href="CSV/Table.html"><code>CSV::Table</code></a>, consisting of <a href="CSV/Row.html"><code>CSV::Row</code></a>.</p>

<pre class="ruby"><span class="ruby-comment"># Headers are part of data</span>
<span class="ruby-identifier">data</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">&lt;&lt;~ROWS</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-value">  Name,Department,Salary
  Bob,Engineering,1000
  Jane,Sales,2000
  John,Management,5000
</span><span class="ruby-identifier">ROWS</span>

<span class="ruby-identifier">data</span>.<span class="ruby-identifier">class</span>      <span class="ruby-comment">#=&gt; CSV::Table</span>
<span class="ruby-identifier">data</span>.<span class="ruby-identifier">first</span>      <span class="ruby-comment">#=&gt; #&lt;CSV::Row &quot;Name&quot;:&quot;Bob&quot; &quot;Department&quot;:&quot;Engineering&quot; &quot;Salary&quot;:&quot;1000&quot;&gt;</span>
<span class="ruby-identifier">data</span>.<span class="ruby-identifier">first</span>.<span class="ruby-identifier">to_h</span> <span class="ruby-comment">#=&gt; {&quot;Name&quot;=&gt;&quot;Bob&quot;, &quot;Department&quot;=&gt;&quot;Engineering&quot;, &quot;Salary&quot;=&gt;&quot;1000&quot;}</span>

<span class="ruby-comment"># Headers provided by developer</span>
<span class="ruby-identifier">data</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&#39;Bob,Engineering,1000&#39;</span>, <span class="ruby-value">headers:</span> <span class="ruby-node">%i[name department salary]</span>)
<span class="ruby-identifier">data</span>.<span class="ruby-identifier">first</span>      <span class="ruby-comment">#=&gt; #&lt;CSV::Row name:&quot;Bob&quot; department:&quot;Engineering&quot; salary:&quot;1000&quot;&gt;</span>
</pre>

<h3 id="class-CSV-label-Converters">Converters<span><a href="#class-CSV-label-Converters">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>By default, each value (field or header) parsed by CSV is formed into a String. You can use a <em>field</em> <em>converter</em> or  <em>header</em> <em>converter</em> to intercept and modify the parsed values:</p>
<ul><li>
<p>See <a href="#class-CSV-label-Field+Converters">Field Converters</a>.</p>
</li><li>
<p>See <a href="#class-CSV-label-Header+Converters">Header Converters</a>.</p>
</li></ul>

<p>Also by default, each value to be written during generation is written ‘as-is’. You can use a <em>write</em> <em>converter</em> to modify values before writing.</p>
<ul><li>
<p>See <a href="#class-CSV-label-Write+Converters">Write Converters</a>.</p>
</li></ul>

<h4 id="class-CSV-label-Specifying+Converters">Specifying Converters<span><a href="#class-CSV-label-Specifying+Converters">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>You can specify converters for parsing or generating in the <code>options</code> argument to various CSV methods:</p>
<ul><li>
<p>Option <code>converters</code> for converting parsed field values.</p>
</li><li>
<p>Option <code>header_converters</code> for converting parsed header values.</p>
</li><li>
<p>Option <code>write_converters</code> for converting values to be written (generated).</p>
</li></ul>

<p>There are three forms for specifying converters:</p>
<ul><li>
<p>A converter proc: executable code to be used for conversion.</p>
</li><li>
<p>A converter name: the name of a stored converter.</p>
</li><li>
<p>A converter list: an array of converter procs, converter names, and converter lists.</p>
</li></ul>

<h5 id="class-CSV-label-Converter+Procs">Converter Procs<span><a href="#class-CSV-label-Converter+Procs">&para;</a> <a href="#top">&uarr;</a></span></h5>

<p>This converter proc, <code>strip_converter</code>, accepts a value <code>field</code> and returns <code>field.strip</code>:</p>

<pre class="ruby"><span class="ruby-identifier">strip_converter</span> = <span class="ruby-identifier">proc</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">field</span><span class="ruby-operator">|</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">strip</span> }
</pre>

<p>In this call to <code>CSV.parse</code>, the keyword argument <code>converters: string_converter</code> specifies that:</p>
<ul><li>
<p>Proc <code>string_converter</code> is to be called for each parsed field.</p>
</li><li>
<p>The converter’s return value is to replace the <code>field</code> value.</p>
</li></ul>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot; foo , 0 \n bar , 1 \n baz , 2 \n&quot;</span>
<span class="ruby-identifier">array</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">converters:</span> <span class="ruby-identifier">strip_converter</span>)
<span class="ruby-identifier">array</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>A converter proc can receive a second argument, <code>field_info</code>, that contains details about the field. This modified <code>strip_converter</code> displays its arguments:</p>

<pre class="ruby"><span class="ruby-identifier">strip_converter</span> = <span class="ruby-identifier">proc</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">field</span>, <span class="ruby-identifier">field_info</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> [<span class="ruby-identifier">field</span>, <span class="ruby-identifier">field_info</span>]
  <span class="ruby-identifier">field</span>.<span class="ruby-identifier">strip</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">string</span> = <span class="ruby-string">&quot; foo , 0 \n bar , 1 \n baz , 2 \n&quot;</span>
<span class="ruby-identifier">array</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">converters:</span> <span class="ruby-identifier">strip_converter</span>)
<span class="ruby-identifier">array</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Output:</p>

<pre>[&quot; foo &quot;, #&lt;struct CSV::FieldInfo index=0, line=1, header=nil&gt;]
[&quot; 0 &quot;, #&lt;struct CSV::FieldInfo index=1, line=1, header=nil&gt;]
[&quot; bar &quot;, #&lt;struct CSV::FieldInfo index=0, line=2, header=nil&gt;]
[&quot; 1 &quot;, #&lt;struct CSV::FieldInfo index=1, line=2, header=nil&gt;]
[&quot; baz &quot;, #&lt;struct CSV::FieldInfo index=0, line=3, header=nil&gt;]
[&quot; 2 &quot;, #&lt;struct CSV::FieldInfo index=1, line=3, header=nil&gt;]</pre>

<p>Each <a href="CSV.html#FieldInfo"><code>CSV::FieldInfo</code></a> object shows:</p>
<ul><li>
<p>The 0-based field index.</p>
</li><li>
<p>The 1-based line index.</p>
</li><li>
<p>The field header, if any.</p>
</li></ul>

<h5 id="class-CSV-label-Stored+Converters">Stored Converters<span><a href="#class-CSV-label-Stored+Converters">&para;</a> <a href="#top">&uarr;</a></span></h5>

<p>A converter may be given a name and stored in a structure where the parsing methods can find it by name.</p>

<p>The storage structure for field converters is the Hash <a href="CSV.html#Converters"><code>CSV::Converters</code></a>. It has several built-in converter procs:</p>
<ul><li>
<p><code>:integer</code>: converts each String-embedded integer into a true Integer.</p>
</li><li>
<p><code>:float</code>: converts each String-embedded float into a true Float.</p>
</li><li>
<p><code>:date</code>: converts each String-embedded date into a true Date.</p>
</li><li>
<p><code>:date_time</code>: converts each String-embedded date-time into a true DateTime</p>
</li></ul>

<p>. This example creates a converter proc, then stores it:</p>

<pre class="ruby"><span class="ruby-identifier">strip_converter</span> = <span class="ruby-identifier">proc</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">field</span><span class="ruby-operator">|</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">strip</span> }
<span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">Converters</span>[<span class="ruby-value">:strip</span>] = <span class="ruby-identifier">strip_converter</span>
</pre>

<p>Then the parsing method call can refer to the converter by its name, <code>:strip</code>:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot; foo , 0 \n bar , 1 \n baz , 2 \n&quot;</span>
<span class="ruby-identifier">array</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">converters:</span> <span class="ruby-value">:strip</span>)
<span class="ruby-identifier">array</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>The storage structure for header converters is the Hash <a href="CSV.html#HeaderConverters"><code>CSV::HeaderConverters</code></a>, which works in the same way. It also has built-in converter procs:</p>
<ul><li>
<p><code>:downcase</code>: Downcases each header.</p>
</li><li>
<p><code>:symbol</code>: Converts each header to a Symbol.</p>
</li></ul>

<p>There is no such storage structure for write headers.</p>

<p>In order for the parsing methods to access stored converters in non-main-Ractors, the storage structure must be made shareable first. Therefore, <code>Ractor.make_shareable(CSV::Converters)</code> and <code>Ractor.make_shareable(CSV::HeaderConverters)</code> must be called before the creation of Ractors that use the converters stored in these structures. (Since making the storage structures shareable involves freezing them, any custom converters that are to be used must be added first.)</p>

<h5 id="class-CSV-label-Converter+Lists">Converter Lists<span><a href="#class-CSV-label-Converter+Lists">&para;</a> <a href="#top">&uarr;</a></span></h5>

<p>A <em>converter</em> <em>list</em> is an Array that may include any assortment of:</p>
<ul><li>
<p>Converter procs.</p>
</li><li>
<p>Names of stored converters.</p>
</li><li>
<p>Nested converter lists.</p>
</li></ul>

<p>Examples:</p>

<pre class="ruby"><span class="ruby-identifier">numeric_converters</span> = [<span class="ruby-value">:integer</span>, <span class="ruby-value">:float</span>]
<span class="ruby-identifier">date_converters</span> = [<span class="ruby-value">:date</span>, <span class="ruby-value">:date_time</span>]
[<span class="ruby-identifier">numeric_converters</span>, <span class="ruby-identifier">strip_converter</span>]
[<span class="ruby-identifier">strip_converter</span>, <span class="ruby-identifier">date_converters</span>, <span class="ruby-value">:float</span>]
</pre>

<p>Like a converter proc, a converter list may be named and stored in either CSV::Converters or <a href="CSV.html#HeaderConverters"><code>CSV::HeaderConverters</code></a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">Converters</span>[<span class="ruby-value">:custom</span>] = [<span class="ruby-identifier">strip_converter</span>, <span class="ruby-identifier">date_converters</span>, <span class="ruby-value">:float</span>]
<span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">HeaderConverters</span>[<span class="ruby-value">:custom</span>] = [<span class="ruby-value">:downcase</span>, <span class="ruby-value">:symbol</span>]
</pre>

<p>There are two built-in converter lists:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">Converters</span>[<span class="ruby-value">:numeric</span>] <span class="ruby-comment"># =&gt; [:integer, :float]</span>
<span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">Converters</span>[<span class="ruby-value">:all</span>] <span class="ruby-comment"># =&gt; [:date_time, :numeric]</span>
</pre>

<h4 id="class-CSV-label-Field+Converters">Field Converters<span><a href="#class-CSV-label-Field+Converters">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>With no conversion, all parsed fields in all rows become Strings:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; # =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>When you specify a field converter, each parsed field is passed to the converter; its return value becomes the stored value for the field. A converter might, for example, convert an integer embedded in a String into a true Integer. (In fact, that’s what built-in field converter <code>:integer</code> does.)</p>

<p>There are three ways to use field converters.</p>
<ul><li>
<p>Using option <a href="#class-CSV-label-Option+converters">converters</a> with a parsing method:</p>

<pre class="ruby"><span class="ruby-identifier">ary</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">converters:</span> <span class="ruby-value">:integer</span>)
<span class="ruby-identifier">ary</span> <span class="ruby-comment"># =&gt; [0, 1, 2] # =&gt; [[&quot;foo&quot;, 0], [&quot;bar&quot;, 1], [&quot;baz&quot;, 2]]</span>
</pre>
</li><li>
<p>Using option <a href="#class-CSV-label-Option+converters">converters</a> with a new CSV instance:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">converters:</span> <span class="ruby-value">:integer</span>)
<span class="ruby-comment"># Field converters in effect:</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">converters</span> <span class="ruby-comment"># =&gt; [:integer]</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, 0], [&quot;bar&quot;, 1], [&quot;baz&quot;, 2]]</span>
</pre>
</li><li>
<p>Using method <a href="CSV.html#method-i-convert"><code>convert</code></a> to add a field converter to a CSV instance:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-comment"># Add a converter.</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">convert</span>(<span class="ruby-value">:integer</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">converters</span> <span class="ruby-comment"># =&gt; [:integer]</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, 0], [&quot;bar&quot;, 1], [&quot;baz&quot;, 2]]</span>
</pre>
</li></ul>

<p>Installing a field converter does not affect already-read rows:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; [&quot;foo&quot;, &quot;0&quot;]</span>
<span class="ruby-comment"># Add a converter.</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">convert</span>(<span class="ruby-value">:integer</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">converters</span> <span class="ruby-comment"># =&gt; [:integer]</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment"># =&gt; [[&quot;bar&quot;, 1], [&quot;baz&quot;, 2]]</span>
</pre>

<p>There are additional built-in converters, and custom converters are also supported.</p>

<h5 id="class-CSV-label-Built-In+Field+Converters">Built-In Field Converters<span><a href="#class-CSV-label-Built-In+Field+Converters">&para;</a> <a href="#top">&uarr;</a></span></h5>

<p>The built-in field converters are in Hash <a href="CSV.html#Converters"><code>CSV::Converters</code></a>:</p>
<ul><li>
<p>Each key is a field converter name.</p>
</li><li>
<p>Each value is one of:</p>
<ul><li>
<p>A Proc field converter.</p>
</li><li>
<p>An Array of field converter names.</p>
</li></ul>
</li></ul>

<p>Display:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">Converters</span>.<span class="ruby-identifier">each_pair</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">name</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Proc</span>)
    <span class="ruby-identifier">p</span> [<span class="ruby-identifier">name</span>, <span class="ruby-identifier">value</span>.<span class="ruby-identifier">class</span>]
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">p</span> [<span class="ruby-identifier">name</span>, <span class="ruby-identifier">value</span>]
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-value">:integer</span>, <span class="ruby-constant">Proc</span>]
[<span class="ruby-value">:float</span>, <span class="ruby-constant">Proc</span>]
[<span class="ruby-value">:numeric</span>, [<span class="ruby-value">:integer</span>, <span class="ruby-value">:float</span>]]
[<span class="ruby-value">:date</span>, <span class="ruby-constant">Proc</span>]
[<span class="ruby-value">:date_time</span>, <span class="ruby-constant">Proc</span>]
[<span class="ruby-value">:all</span>, [<span class="ruby-value">:date_time</span>, <span class="ruby-value">:numeric</span>]]
</pre>

<p>Each of these converters transcodes values to UTF-8 before attempting conversion. If a value cannot be transcoded to UTF-8 the conversion will fail and the value will remain unconverted.</p>

<p>Converter <code>:integer</code> converts each field that Integer() accepts:</p>

<pre class="ruby"><span class="ruby-identifier">data</span> = <span class="ruby-string">&#39;0,1,2,x&#39;</span>
<span class="ruby-comment"># Without the converter</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">data</span>)
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; [&quot;0&quot;, &quot;1&quot;, &quot;2&quot;, &quot;x&quot;]</span>
<span class="ruby-comment"># With the converter</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">data</span>, <span class="ruby-value">converters:</span> <span class="ruby-value">:integer</span>)
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; [0, 1, 2, &quot;x&quot;]</span>
</pre>

<p>Converter <code>:float</code> converts each field that Float() accepts:</p>

<pre class="ruby"><span class="ruby-identifier">data</span> = <span class="ruby-string">&#39;1.0,3.14159,x&#39;</span>
<span class="ruby-comment"># Without the converter</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">data</span>)
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; [&quot;1.0&quot;, &quot;3.14159&quot;, &quot;x&quot;]</span>
<span class="ruby-comment"># With the converter</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">data</span>, <span class="ruby-value">converters:</span> <span class="ruby-value">:float</span>)
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; [1.0, 3.14159, &quot;x&quot;]</span>
</pre>

<p>Converter <code>:numeric</code> converts with both <code>:integer</code> and <code>:float</code>..</p>

<p>Converter <code>:date</code> converts each field that Date::parse accepts:</p>

<pre class="ruby"><span class="ruby-identifier">data</span> = <span class="ruby-string">&#39;2001-02-03,x&#39;</span>
<span class="ruby-comment"># Without the converter</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">data</span>)
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; [&quot;2001-02-03&quot;, &quot;x&quot;]</span>
<span class="ruby-comment"># With the converter</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">data</span>, <span class="ruby-value">converters:</span> <span class="ruby-value">:date</span>)
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; [#&lt;Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)&gt;, &quot;x&quot;]</span>
</pre>

<p>Converter <code>:date_time</code> converts each field that DateTime::parse accepts:</p>

<pre class="ruby"><span class="ruby-identifier">data</span> = <span class="ruby-string">&#39;2020-05-07T14:59:00-05:00,x&#39;</span>
<span class="ruby-comment"># Without the converter</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">data</span>)
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; [&quot;2020-05-07T14:59:00-05:00&quot;, &quot;x&quot;]</span>
<span class="ruby-comment"># With the converter</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">data</span>, <span class="ruby-value">converters:</span> <span class="ruby-value">:date_time</span>)
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; [#&lt;DateTime: 2020-05-07T14:59:00-05:00 ((2458977j,71940s,0n),-18000s,2299161j)&gt;, &quot;x&quot;]</span>
</pre>

<p>Converter <code>:numeric</code> converts with both <code>:date_time</code> and <code>:numeric</code>..</p>

<p>As seen above, method <a href="CSV.html#method-i-convert"><code>convert</code></a> adds converters to a CSV instance, and method <a href="CSV.html#method-i-converters"><code>converters</code></a> returns an Array of the converters in effect:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;0,1,2&#39;</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">converters</span> <span class="ruby-comment"># =&gt; []</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">convert</span>(<span class="ruby-value">:integer</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">converters</span> <span class="ruby-comment"># =&gt; [:integer]</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">convert</span>(<span class="ruby-value">:date</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">converters</span> <span class="ruby-comment"># =&gt; [:integer, :date]</span>
</pre>

<h5 id="class-CSV-label-Custom+Field+Converters">Custom Field Converters<span><a href="#class-CSV-label-Custom+Field+Converters">&para;</a> <a href="#top">&uarr;</a></span></h5>

<p>You can define a custom field converter:</p>

<pre class="ruby"><span class="ruby-identifier">strip_converter</span> = <span class="ruby-identifier">proc</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">field</span><span class="ruby-operator">|</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">strip</span> }
<span class="ruby-identifier">string</span> = <span class="ruby-string">&quot; foo , 0 \n bar , 1 \n baz , 2 \n&quot;</span>
<span class="ruby-identifier">array</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">converters:</span> <span class="ruby-identifier">strip_converter</span>)
<span class="ruby-identifier">array</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>You can register the converter in Converters Hash, which allows you to refer to it by name:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">Converters</span>[<span class="ruby-value">:strip</span>] = <span class="ruby-identifier">strip_converter</span>
<span class="ruby-identifier">string</span> = <span class="ruby-string">&quot; foo , 0 \n bar , 1 \n baz , 2 \n&quot;</span>
<span class="ruby-identifier">array</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">converters:</span> <span class="ruby-value">:strip</span>)
<span class="ruby-identifier">array</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<h4 id="class-CSV-label-Header+Converters">Header Converters<span><a href="#class-CSV-label-Header+Converters">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>Header converters operate only on headers (and not on other rows).</p>

<p>There are three ways to use header converters; these examples use built-in header converter <code>:downcase</code>, which downcases each parsed header.</p>
<ul><li>
<p>Option <code>header_converters</code> with a singleton parsing method:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Count\nFoo,0\n,Bar,1\nBaz,2&quot;</span>
<span class="ruby-identifier">tbl</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>, <span class="ruby-value">header_converters:</span> <span class="ruby-value">:downcase</span>)
<span class="ruby-identifier">tbl</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; CSV::Table</span>
<span class="ruby-identifier">tbl</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [&quot;name&quot;, &quot;count&quot;]</span>
</pre>
</li><li>
<p>Option <code>header_converters</code> with a new CSV instance:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">header_converters:</span> <span class="ruby-value">:downcase</span>)
<span class="ruby-comment"># Header converters in effect:</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">header_converters</span> <span class="ruby-comment"># =&gt; [:downcase]</span>
<span class="ruby-identifier">tbl</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">tbl</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [&quot;Name&quot;, &quot;Count&quot;]</span>
</pre>
</li><li>
<p>Method <a href="CSV.html#method-i-header_convert"><code>header_convert</code></a> adds a header converter to a CSV instance:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-comment"># Add a header converter.</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">header_convert</span>(<span class="ruby-value">:downcase</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">header_converters</span> <span class="ruby-comment"># =&gt; [:downcase]</span>
<span class="ruby-identifier">tbl</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">tbl</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [&quot;Name&quot;, &quot;Count&quot;]</span>
</pre>
</li></ul>

<h5 id="class-CSV-label-Built-In+Header+Converters">Built-In Header Converters<span><a href="#class-CSV-label-Built-In+Header+Converters">&para;</a> <a href="#top">&uarr;</a></span></h5>

<p>The built-in header converters are in Hash <a href="CSV.html#HeaderConverters"><code>CSV::HeaderConverters</code></a>. The keys there are the names of the converters:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">HeaderConverters</span>.<span class="ruby-identifier">keys</span> <span class="ruby-comment"># =&gt; [:downcase, :symbol]</span>
</pre>

<p>Converter <code>:downcase</code> converts each header by downcasing it:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Count\nFoo,0\n,Bar,1\nBaz,2&quot;</span>
<span class="ruby-identifier">tbl</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>, <span class="ruby-value">header_converters:</span> <span class="ruby-value">:downcase</span>)
<span class="ruby-identifier">tbl</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; CSV::Table</span>
<span class="ruby-identifier">tbl</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [&quot;name&quot;, &quot;count&quot;]</span>
</pre>

<p>Converter <code>:symbol</code> converts each header by making it into a Symbol:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Count\nFoo,0\n,Bar,1\nBaz,2&quot;</span>
<span class="ruby-identifier">tbl</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>, <span class="ruby-value">header_converters:</span> <span class="ruby-value">:symbol</span>)
<span class="ruby-identifier">tbl</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [:name, :count]</span>
</pre>

<p>Details:</p>
<ul><li>
<p>Strips leading and trailing whitespace.</p>
</li><li>
<p>Downcases the header.</p>
</li><li>
<p>Replaces embedded spaces with underscores.</p>
</li><li>
<p>Removes non-word characters.</p>
</li><li>
<p>Makes the string into a Symbol.</p>
</li></ul>

<h5 id="class-CSV-label-Custom+Header+Converters">Custom Header Converters<span><a href="#class-CSV-label-Custom+Header+Converters">&para;</a> <a href="#top">&uarr;</a></span></h5>

<p>You can define a custom header converter:</p>

<pre class="ruby"><span class="ruby-identifier">upcase_converter</span> = <span class="ruby-identifier">proc</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">header</span><span class="ruby-operator">|</span> <span class="ruby-identifier">header</span>.<span class="ruby-identifier">upcase</span> }
<span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">table</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>, <span class="ruby-value">header_converters:</span> <span class="ruby-identifier">upcase_converter</span>)
<span class="ruby-identifier">table</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Table mode:col_or_row row_count:4&gt;</span>
<span class="ruby-identifier">table</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [&quot;NAME&quot;, &quot;VALUE&quot;]</span>
</pre>

<p>You can register the converter in HeaderConverters Hash, which allows you to refer to it by name:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">HeaderConverters</span>[<span class="ruby-value">:upcase</span>] = <span class="ruby-identifier">upcase_converter</span>
<span class="ruby-identifier">table</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>, <span class="ruby-value">header_converters:</span> <span class="ruby-value">:upcase</span>)
<span class="ruby-identifier">table</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Table mode:col_or_row row_count:4&gt;</span>
<span class="ruby-identifier">table</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [&quot;NAME&quot;, &quot;VALUE&quot;]</span>
</pre>

<h5 id="class-CSV-label-Write+Converters">Write Converters<span><a href="#class-CSV-label-Write+Converters">&para;</a> <a href="#top">&uarr;</a></span></h5>

<p>When you specify a write converter for generating CSV, each field to be written is passed to the converter; its return value becomes the new value for the field. A converter might, for example, strip whitespace from a field.</p>

<p>Using no write converter (all fields unmodified):</p>

<pre class="ruby"><span class="ruby-identifier">output_string</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39; foo &#39;</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39; bar &#39;</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39; baz &#39;</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">output_string</span> <span class="ruby-comment"># =&gt; &quot; foo ,0\n bar ,1\n baz ,2\n&quot;</span>
</pre>

<p>Using option <code>write_converters</code> with two custom write converters:</p>

<pre class="ruby"><span class="ruby-identifier">strip_converter</span> = <span class="ruby-identifier">proc</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">field</span><span class="ruby-operator">|</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:strip</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">strip</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">field</span> }
<span class="ruby-identifier">upcase_converter</span> = <span class="ruby-identifier">proc</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">field</span><span class="ruby-operator">|</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:upcase</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">upcase</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">field</span> }
<span class="ruby-identifier">write_converters</span> = [<span class="ruby-identifier">strip_converter</span>, <span class="ruby-identifier">upcase_converter</span>]
<span class="ruby-identifier">output_string</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">write_converters:</span> <span class="ruby-identifier">write_converters</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39; foo &#39;</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39; bar &#39;</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39; baz &#39;</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">output_string</span> <span class="ruby-comment"># =&gt; &quot;FOO,0\nBAR,1\nBAZ,2\n&quot;</span>
</pre>

<h3 id="class-CSV-label-Character+Encodings+-28M17n+or+Multilingualization-29">Character Encodings (M17n or Multilingualization)<span><a href="#class-CSV-label-Character+Encodings+-28M17n+or+Multilingualization-29">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>This new <a href="CSV.html"><code>CSV</code></a> parser is m17n savvy.  The parser works in the Encoding of the IO or String object being read from or written to. Your data is never transcoded (unless you ask Ruby to transcode it for you) and will literally be parsed in the Encoding it is in. Thus <a href="CSV.html"><code>CSV</code></a> will return Arrays or Rows of Strings in the Encoding of your data. This is accomplished by transcoding the parser itself into your Encoding.</p>

<p>Some transcoding must take place, of course, to accomplish this multiencoding support. For example, <code>:col_sep</code>, <code>:row_sep</code>, and <code>:quote_char</code> must be transcoded to match your data.  Hopefully this makes the entire process feel transparent, since CSV’s defaults should just magically work for your data. However, you can set these values manually in the target Encoding to avoid the translation.</p>

<p>It’s also important to note that while all of CSV’s core parser is now Encoding agnostic, some features are not. For example, the built-in converters will try to transcode data to UTF-8 before making conversions. Again, you can provide custom converters that are aware of your Encodings to avoid this translation. It’s just too hard for me to support native conversions in all of Ruby’s Encodings.</p>

<p>Anyway, the practical side of this is simple: make sure IO and String objects passed into <a href="CSV.html"><code>CSV</code></a> have the proper Encoding set and everything should just work. <a href="CSV.html"><code>CSV</code></a> methods that allow you to open IO objects (<a href="CSV.html#method-c-foreach"><code>CSV::foreach()</code></a>, <a href="CSV.html#method-c-open"><code>CSV::open()</code></a>, <a href="CSV.html#method-c-read"><code>CSV::read()</code></a>, and <a href="CSV.html#method-c-readlines"><code>CSV::readlines()</code></a>) do allow you to specify the Encoding.</p>

<p>One minor exception comes when generating <a href="CSV.html"><code>CSV</code></a> into a String with an Encoding that is not ASCII compatible. There’s no existing data for <a href="CSV.html"><code>CSV</code></a> to use to prepare itself and thus you will probably need to manually specify the desired Encoding for most of those cases. It will try to guess using the fields in a row of output though, when using <a href="CSV.html#method-c-generate_line"><code>CSV::generate_line()</code></a> or Array#to_csv().</p>

<p>I try to point out any other Encoding issues in the documentation of methods as they come up.</p>

<p>This has been tested to the best of my ability with all non-“dummy” Encodings Ruby ships with. However, it is brave new code and may have some bugs. Please feel free to <a href="mailto:james@grayproductions.net">report</a> any issues you find with it.</p>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">


      <section class="constants-list">
      <header>
      <h3>Constants</h3>
      </header>
      <dl>
          <dt id="ConverterEncoding">ConverterEncoding
          <dd><p>The encoding used by all converters.</p>
          <dt id="Converters">Converters
          <dd><p>A Hash containing the names and Procs for the built-in field converters. See <a href="#class-CSV-label-Built-In+Field+Converters">Built-In Field Converters</a>.</p>

<p>This Hash is intentionally left unfrozen, and may be extended with custom field converters. See <a href="#class-CSV-label-Custom+Field+Converters">Custom Field Converters</a>.</p>
          <dt id="DEFAULT_OPTIONS">DEFAULT_OPTIONS
          <dd><p>Default values for method options.</p>
          <dt id="DateMatcher">DateMatcher
          <dd><p>A Regexp used to find and convert some common Date formats.</p>
          <dt id="DateTimeMatcher">DateTimeMatcher
          <dd><p>A Regexp used to find and convert some common DateTime formats.</p>
          <dt id="FieldInfo">FieldInfo
          <dd><p>A <a href="CSV.html#FieldInfo"><code>FieldInfo</code></a> Struct contains details about a field’s position in the data source it was read from.  <a href="CSV.html"><code>CSV</code></a> will pass this Struct to some blocks that make decisions based on field structure.  See <a href="CSV.html#method-i-convert_fields"><code>CSV.convert_fields()</code></a> for an example.</p>
<dl class="rdoc-list note-list"><dt><strong><code>index</code></strong>
<dd>
<p>The zero-based index of the field in its row.</p>
</dd><dt><strong><code>line</code></strong>
<dd>
<p>The line of the data source this row is from.</p>
</dd><dt><strong><code>header</code></strong>
<dd>
<p>The header for the column, when available.</p>
</dd><dt><strong><code>quoted?</code></strong>
<dd>
<p>True or false, whether the original value is quoted or not.</p>
</dd></dl>
          <dt id="HeaderConverters">HeaderConverters
          <dd><p>A Hash containing the names and Procs for the built-in header converters. See <a href="#class-CSV-label-Built-In+Header+Converters">Built-In Header Converters</a>.</p>

<p>This Hash is intentionally left unfrozen, and may be extended with custom field converters. See <a href="#class-CSV-label-Custom+Header+Converters">Custom Header Converters</a>.</p>
          <dt id="VERSION">VERSION
          <dd><p>The version of the installed library.</p>
        </dl>
        </section>

        <section class="attribute-method-details" class="method-section">
        <header>
        <h3>Attributes</h3>
        </header>

          <div id="attribute-i-encoding" class="method-detail">
            <div class="method-heading attribute-method-heading">
              <span class="method-name">encoding</span><span
                class="attribute-access-type">[R]</span>
            </div>

            <div class="method-description">
              <p>:call-seq:</p>

<pre>csv.encoding -&gt; encoding</pre>

<p>Returns the encoding used for parsing and generating; see <a href="#class-CSV-label-Character+Encodings+-28M17n+or+Multilingualization-29">Character Encodings (M17n or Multilingualization)</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">encoding</span> <span class="ruby-comment"># =&gt; #&lt;Encoding:UTF-8&gt;</span>
</pre>
              </div>
            </div>
            </section>


                <section id="public-class-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Class Methods</h3>
                </header>

                  <div id="method-c-filter" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          filter(in_string_or_io, **options) {|row| ... } &rarr; array_of_arrays or csv_table
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          filter(in_string_or_io, out_string_or_io, **options) {|row| ... } &rarr; array_of_arrays or csv_table
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          filter(**options) {|row| ... } &rarr; array_of_arrays or csv_table
                              </span>
                            </div>

                            <div class="method-description">
                              <ul><li>
<p>Parses CSV from a source (String, IO stream, or ARGF).</p>
</li><li>
<p>Calls the given block with each parsed row:</p>
<ul><li>
<p>Without headers, each row is an Array.</p>
</li><li>
<p>With headers, each row is a <a href="CSV/Row.html"><code>CSV::Row</code></a>.</p>
</li></ul>
</li><li>
<p>Generates CSV to an output (String, IO stream, or STDOUT).</p>
</li><li>
<p>Returns the parsed source:</p>
<ul><li>
<p>Without headers, an Array of Arrays.</p>
</li><li>
<p>With headers, a <a href="CSV/Table.html"><code>CSV::Table</code></a>.</p>
</li></ul>
</li></ul>

<p>When <code>in_string_or_io</code> is given, but not <code>out_string_or_io</code>, parses from the given <code>in_string_or_io</code> and generates to STDOUT.</p>

<p>String input without headers:</p>

<pre class="ruby"><span class="ruby-identifier">in_string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2&quot;</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">filter</span>(<span class="ruby-identifier">in_string</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">row</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">upcase!</span>
  <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>] = <span class="ruby-operator">-</span> <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">to_i</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; [[&quot;FOO&quot;, 0], [&quot;BAR&quot;, -1], [&quot;BAZ&quot;, -2]]</span>
</pre>

<p>Output (to STDOUT):</p>

<pre>FOO,0
BAR,-1
BAZ,-2</pre>

<p>String input with headers:</p>

<pre class="ruby"><span class="ruby-identifier">in_string</span> = <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2&quot;</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">filter</span>(<span class="ruby-identifier">in_string</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">row</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">upcase!</span>
  <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>] = <span class="ruby-operator">-</span> <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">to_i</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Table mode:col_or_row row_count:4&gt;</span>
</pre>

<p>Output (to STDOUT):</p>

<pre>Name,Value
FOO,0
BAR,-1
BAZ,-2</pre>

<p>IO stream input without headers:</p>

<pre class="ruby"><span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-string">&#39;t.csv&#39;</span>, <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2&quot;</span>)
<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">&#39;t.csv&#39;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">in_io</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">filter</span>(<span class="ruby-identifier">in_io</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">row</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">upcase!</span>
    <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>] = <span class="ruby-operator">-</span> <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">to_i</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; [[&quot;FOO&quot;, 0], [&quot;BAR&quot;, -1], [&quot;BAZ&quot;, -2]]</span>
</pre>

<p>Output (to STDOUT):</p>

<pre>FOO,0
BAR,-1
BAZ,-2</pre>

<p>IO stream input with headers:</p>

<pre class="ruby"><span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-string">&#39;t.csv&#39;</span>, <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2&quot;</span>)
<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">&#39;t.csv&#39;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">in_io</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">filter</span>(<span class="ruby-identifier">in_io</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">row</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">upcase!</span>
    <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>] = <span class="ruby-operator">-</span> <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">to_i</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Table mode:col_or_row row_count:4&gt;</span>
</pre>

<p>Output (to STDOUT):</p>

<pre>Name,Value
FOO,0
BAR,-1
BAZ,-2</pre>

<p>When both <code>in_string_or_io</code> and <code>out_string_or_io</code> are given, parses from <code>in_string_or_io</code> and generates to <code>out_string_or_io</code>.</p>

<p>String output without headers:</p>

<pre class="ruby"><span class="ruby-identifier">in_string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2&quot;</span>
<span class="ruby-identifier">out_string</span> = <span class="ruby-string">&#39;&#39;</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">filter</span>(<span class="ruby-identifier">in_string</span>, <span class="ruby-identifier">out_string</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">row</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">upcase!</span>
  <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>] = <span class="ruby-operator">-</span> <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">to_i</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; [[&quot;FOO&quot;, 0], [&quot;BAR&quot;, -1], [&quot;BAZ&quot;, -2]]</span>
<span class="ruby-identifier">out_string</span> <span class="ruby-comment"># =&gt; &quot;FOO,0\nBAR,-1\nBAZ,-2\n&quot;</span>
</pre>

<p>String output with headers:</p>

<pre class="ruby"><span class="ruby-identifier">in_string</span> = <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2&quot;</span>
<span class="ruby-identifier">out_string</span> = <span class="ruby-string">&#39;&#39;</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">filter</span>(<span class="ruby-identifier">in_string</span>, <span class="ruby-identifier">out_string</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">row</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">upcase!</span>
  <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>] = <span class="ruby-operator">-</span> <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">to_i</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Table mode:col_or_row row_count:4&gt;</span>
<span class="ruby-identifier">out_string</span> <span class="ruby-comment"># =&gt; &quot;Name,Value\nFOO,0\nBAR,-1\nBAZ,-2\n&quot;</span>
</pre>

<p>IO stream output without headers:</p>

<pre class="ruby"><span class="ruby-identifier">in_string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2&quot;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">&#39;t.csv&#39;</span>, <span class="ruby-string">&#39;w&#39;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">out_io</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">filter</span>(<span class="ruby-identifier">in_string</span>, <span class="ruby-identifier">out_io</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">row</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">upcase!</span>
    <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>] = <span class="ruby-operator">-</span> <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">to_i</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; [[&quot;FOO&quot;, 0], [&quot;BAR&quot;, -1], [&quot;BAZ&quot;, -2]]</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span>(<span class="ruby-string">&#39;t.csv&#39;</span>) <span class="ruby-comment"># =&gt; &quot;FOO,0\nBAR,-1\nBAZ,-2\n&quot;</span>
</pre>

<p>IO stream output with headers:</p>

<pre class="ruby"><span class="ruby-identifier">in_string</span> = <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2&quot;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">&#39;t.csv&#39;</span>, <span class="ruby-string">&#39;w&#39;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">out_io</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">filter</span>(<span class="ruby-identifier">in_string</span>, <span class="ruby-identifier">out_io</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">row</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">upcase!</span>
    <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>] = <span class="ruby-operator">-</span> <span class="ruby-identifier">row</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">to_i</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Table mode:col_or_row row_count:4&gt;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span>(<span class="ruby-string">&#39;t.csv&#39;</span>) <span class="ruby-comment"># =&gt; &quot;Name,Value\nFOO,0\nBAR,-1\nBAZ,-2\n&quot;</span>
</pre>

<p>When neither <code>in_string_or_io</code> nor <code>out_string_or_io</code> given, parses from ARGF and generates to STDOUT.</p>

<p>Without headers:</p>

<pre class="ruby"><span class="ruby-comment"># Put Ruby code into a file.</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-identifier">&lt;&lt;-EOT</span>
<span class="ruby-value">  require &#39;csv&#39;
  CSV.filter do |row|
    row[0].upcase!
    row[1] = - row[1].to_i
  end
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-string">&#39;t.rb&#39;</span>, <span class="ruby-identifier">ruby</span>)
<span class="ruby-comment"># Put some CSV into a file.</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-string">&#39;t.csv&#39;</span>, <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2&quot;</span>)
<span class="ruby-comment"># Run the Ruby code with CSV filename as argument.</span>
<span class="ruby-identifier">system</span>(<span class="ruby-constant">Gem</span>.<span class="ruby-identifier">ruby</span>, <span class="ruby-string">&quot;t.rb&quot;</span>, <span class="ruby-string">&quot;t.csv&quot;</span>)
</pre>

<p>Output (to STDOUT):</p>

<pre>FOO,0
BAR,-1
BAZ,-2</pre>

<p>With headers:</p>

<pre class="ruby"><span class="ruby-comment"># Put Ruby code into a file.</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-identifier">&lt;&lt;-EOT</span>
<span class="ruby-value">  require &#39;csv&#39;
  CSV.filter(headers: true) do |row|
    row[0].upcase!
    row[1] = - row[1].to_i
  end
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-string">&#39;t.rb&#39;</span>, <span class="ruby-identifier">ruby</span>)
<span class="ruby-comment"># Put some CSV into a file.</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-string">&#39;t.csv&#39;</span>, <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2&quot;</span>)
<span class="ruby-comment"># Run the Ruby code with CSV filename as argument.</span>
<span class="ruby-identifier">system</span>(<span class="ruby-constant">Gem</span>.<span class="ruby-identifier">ruby</span>, <span class="ruby-string">&quot;t.rb&quot;</span>, <span class="ruby-string">&quot;t.csv&quot;</span>)
</pre>

<p>Output (to STDOUT):</p>

<pre>Name,Value
FOO,0
BAR,-1
BAZ,-2</pre>

<p>Arguments:</p>
<ul><li>
<p>Argument <code>in_string_or_io</code> must be a String or an IO stream.</p>
</li><li>
<p>Argument <code>out_string_or_io</code> must be a String or an IO stream.</p>
</li><li>
<p>Arguments <code>**options</code> must be keyword options. See <a href="#class-CSV-label-Options+for+Parsing">Options for Parsing</a>.</p>
</li></ul>

                              <div class="method-source-code" id="filter-source">
            <pre><span class="ruby-comment"># File csv.rb, line 1201</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">filter</span>(<span class="ruby-identifier">input</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">output</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
  <span class="ruby-comment"># parse options for input, output, or both</span>
  <span class="ruby-identifier">in_options</span>, <span class="ruby-identifier">out_options</span> = <span class="ruby-constant">Hash</span>.<span class="ruby-identifier">new</span>, {<span class="ruby-value">row_sep:</span> <span class="ruby-constant">InputRecordSeparator</span>.<span class="ruby-identifier">value</span>}
  <span class="ruby-identifier">options</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">case</span> <span class="ruby-identifier">key</span>
    <span class="ruby-keyword">when</span> <span class="ruby-regexp">/\Ain(?:put)?_(.+)\Z/</span>
      <span class="ruby-identifier">in_options</span>[<span class="ruby-node">$1</span>.<span class="ruby-identifier">to_sym</span>] = <span class="ruby-identifier">value</span>
    <span class="ruby-keyword">when</span> <span class="ruby-regexp">/\Aout(?:put)?_(.+)\Z/</span>
      <span class="ruby-identifier">out_options</span>[<span class="ruby-node">$1</span>.<span class="ruby-identifier">to_sym</span>] = <span class="ruby-identifier">value</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">in_options</span>[<span class="ruby-identifier">key</span>]  = <span class="ruby-identifier">value</span>
      <span class="ruby-identifier">out_options</span>[<span class="ruby-identifier">key</span>] = <span class="ruby-identifier">value</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-comment"># build input and output wrappers</span>
  <span class="ruby-identifier">input</span>  = <span class="ruby-identifier">new</span>(<span class="ruby-identifier">input</span>  <span class="ruby-operator">||</span> <span class="ruby-constant">ARGF</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">in_options</span>)
  <span class="ruby-identifier">output</span> = <span class="ruby-identifier">new</span>(<span class="ruby-identifier">output</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">$stdout</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">out_options</span>)

  <span class="ruby-comment"># process headers</span>
  <span class="ruby-identifier">need_manual_header_output</span> =
    (<span class="ruby-identifier">in_options</span>[<span class="ruby-value">:headers</span>] <span class="ruby-keyword">and</span>
     <span class="ruby-identifier">out_options</span>[<span class="ruby-value">:headers</span>] <span class="ruby-operator">==</span> <span class="ruby-keyword">true</span> <span class="ruby-keyword">and</span>
     <span class="ruby-identifier">out_options</span>[<span class="ruby-value">:write_headers</span>])
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">need_manual_header_output</span>
    <span class="ruby-identifier">first_row</span> = <span class="ruby-identifier">input</span>.<span class="ruby-identifier">shift</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">first_row</span>
      <span class="ruby-keyword">if</span> <span class="ruby-identifier">first_row</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Row</span>)
        <span class="ruby-identifier">headers</span> = <span class="ruby-identifier">first_row</span>.<span class="ruby-identifier">headers</span>
        <span class="ruby-keyword">yield</span> <span class="ruby-identifier">headers</span>
        <span class="ruby-identifier">output</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">headers</span>
      <span class="ruby-keyword">end</span>
      <span class="ruby-keyword">yield</span> <span class="ruby-identifier">first_row</span>
      <span class="ruby-identifier">output</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">first_row</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-comment"># read, yield, write</span>
  <span class="ruby-identifier">input</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">yield</span> <span class="ruby-identifier">row</span>
    <span class="ruby-identifier">output</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">row</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-foreach" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          foreach(path_or_io, mode=&#39;r&#39;, **options) {|row| ... )
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          foreach(path_or_io, mode=&#39;r&#39;, **options) &rarr; new_enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Calls the block with each row read from source <code>path_or_io</code>.</p>

<p>Path input without headers:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">in_path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">in_path</span>, <span class="ruby-identifier">string</span>)
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">foreach</span>(<span class="ruby-identifier">in_path</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">row</span> }
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-string">&quot;foo&quot;</span>, <span class="ruby-string">&quot;0&quot;</span>]
[<span class="ruby-string">&quot;bar&quot;</span>, <span class="ruby-string">&quot;1&quot;</span>]
[<span class="ruby-string">&quot;baz&quot;</span>, <span class="ruby-string">&quot;2&quot;</span>]
</pre>

<p>Path input with headers:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">in_path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">in_path</span>, <span class="ruby-identifier">string</span>)
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">foreach</span>(<span class="ruby-identifier">in_path</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">row</span> }
</pre>

<p>Output:</p>

<pre>&lt;CSV::Row &quot;Name&quot;:&quot;foo&quot; &quot;Value&quot;:&quot;0&quot;&gt;
&lt;CSV::Row &quot;Name&quot;:&quot;bar&quot; &quot;Value&quot;:&quot;1&quot;&gt;
&lt;CSV::Row &quot;Name&quot;:&quot;baz&quot; &quot;Value&quot;:&quot;2&quot;&gt;</pre>

<p>IO stream input without headers:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">&#39;t.csv&#39;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">in_io</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">foreach</span>(<span class="ruby-identifier">in_io</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">row</span> }
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-string">&quot;foo&quot;</span>, <span class="ruby-string">&quot;0&quot;</span>]
[<span class="ruby-string">&quot;bar&quot;</span>, <span class="ruby-string">&quot;1&quot;</span>]
[<span class="ruby-string">&quot;baz&quot;</span>, <span class="ruby-string">&quot;2&quot;</span>]
</pre>

<p>IO stream input with headers:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">&#39;t.csv&#39;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">in_io</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">foreach</span>(<span class="ruby-identifier">in_io</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">row</span> }
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre>&lt;CSV::Row &quot;Name&quot;:&quot;foo&quot; &quot;Value&quot;:&quot;0&quot;&gt;
&lt;CSV::Row &quot;Name&quot;:&quot;bar&quot; &quot;Value&quot;:&quot;1&quot;&gt;
&lt;CSV::Row &quot;Name&quot;:&quot;baz&quot; &quot;Value&quot;:&quot;2&quot;&gt;</pre>

<p>With no block given, returns an Enumerator:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">foreach</span>(<span class="ruby-identifier">path</span>) <span class="ruby-comment"># =&gt; #&lt;Enumerator: CSV:foreach(&quot;t.csv&quot;, &quot;r&quot;)&gt;</span>
</pre>

<p>Arguments:</p>
<ul><li>
<p>Argument <code>path_or_io</code> must be a file path or an IO stream.</p>
</li><li>
<p>Argument <code>mode</code>, if given, must be a File mode See <a href="https://ruby-doc.org/core/IO.html#method-c-new-label-Open+Mode">Open Mode</a>.</p>
</li><li>
<p>Arguments <code>**options</code> must be keyword options. See <a href="#class-CSV-label-Options+for+Parsing">Options for Parsing</a>.</p>
</li><li>
<p>This method optionally accepts an additional <code>:encoding</code> option that you can use to specify the Encoding of the data read from <code>path</code> or <code>io</code>. You must provide this unless your data is in the encoding given by <code>Encoding::default_external</code>. Parsing will use this to determine how to parse the data. You may provide a second Encoding to have the data transcoded as it is read. For example,</p>

<pre>encoding: &#39;UTF-32BE:UTF-8&#39;</pre>

<p>would read <code>UTF-32BE</code> data from the file but transcode it to <code>UTF-8</code> before parsing.</p>
</li></ul>

                              <div class="method-source-code" id="foreach-source">
            <pre><span class="ruby-comment"># File csv.rb, line 1331</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">foreach</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">mode</span>=<span class="ruby-string">&quot;r&quot;</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">to_enum</span>(<span class="ruby-identifier">__method__</span>, <span class="ruby-identifier">path</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">block_given?</span>
  <span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">each</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-generate" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          generate(csv_string, **options) {|csv| ... }
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          generate(**options) {|csv| ... }
                              </span>
                            </div>

                            <div class="method-description">
                              <ul><li>
<p>Argument <code>csv_string</code>, if given, must be a String object; defaults to a new empty String.</p>
</li><li>
<p>Arguments <code>options</code>, if given, should be generating options. See <a href="#class-CSV-label-Options+for+Generating">Options for Generating</a>.</p>
</li></ul>
<hr>

<p>Creates a new CSV object via <code>CSV.new(csv_string, **options)</code>; calls the block with the CSV object, which the block may modify; returns the String generated from the CSV object.</p>

<p>Note that a passed String <strong>is</strong> modified by this method. Pass <code>csv_string</code>.dup if the String must be preserved.</p>

<p>This method has one additional option: <code>:encoding</code>, which sets the base Encoding for the output if no no <code>str</code> is specified. <a href="CSV.html"><code>CSV</code></a> needs this hint if you plan to output non-ASCII compatible data.</p>
<hr>

<p>Add lines:</p>

<pre class="ruby"><span class="ruby-identifier">input_string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">output_string</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">input_string</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;bat&#39;</span>, <span class="ruby-value">3</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;bam&#39;</span>, <span class="ruby-value">4</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">output_string</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\nbat,3\nbam,4\n&quot;</span>
<span class="ruby-identifier">input_string</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\nbat,3\nbam,4\n&quot;</span>
<span class="ruby-identifier">output_string</span>.<span class="ruby-identifier">equal?</span>(<span class="ruby-identifier">input_string</span>) <span class="ruby-comment"># =&gt; true # Same string, modified</span>
</pre>

<p>Add lines into new string, preserving old string:</p>

<pre class="ruby"><span class="ruby-identifier">input_string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">output_string</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">input_string</span>.<span class="ruby-identifier">dup</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;bat&#39;</span>, <span class="ruby-value">3</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;bam&#39;</span>, <span class="ruby-value">4</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">output_string</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\nbat,3\nbam,4\n&quot;</span>
<span class="ruby-identifier">input_string</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">output_string</span>.<span class="ruby-identifier">equal?</span>(<span class="ruby-identifier">input_string</span>) <span class="ruby-comment"># =&gt; false # Different strings</span>
</pre>

<p>Create lines from nothing:</p>

<pre class="ruby"><span class="ruby-identifier">output_string</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;bar&#39;</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;baz&#39;</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">output_string</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
</pre>
<hr>

<p>Raises an exception if <code>csv_string</code> is not a String object:</p>

<pre class="ruby"><span class="ruby-comment"># Raises TypeError (no implicit conversion of Integer into String)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">0</span>)
</pre>

                              <div class="method-source-code" id="generate-source">
            <pre><span class="ruby-comment"># File csv.rb, line 1397</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">generate</span>(<span class="ruby-identifier">str</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">encoding</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:encoding</span>]
  <span class="ruby-comment"># add a default empty String, if none was given</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">str</span>
    <span class="ruby-identifier">str</span> = <span class="ruby-constant">StringIO</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">str</span>)
    <span class="ruby-identifier">str</span>.<span class="ruby-identifier">seek</span>(<span class="ruby-value">0</span>, <span class="ruby-constant">IO</span><span class="ruby-operator">::</span><span class="ruby-constant">SEEK_END</span>)
    <span class="ruby-identifier">str</span>.<span class="ruby-identifier">set_encoding</span>(<span class="ruby-identifier">encoding</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">encoding</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">str</span> = <span class="ruby-string">+&quot;&quot;</span>
    <span class="ruby-identifier">str</span>.<span class="ruby-identifier">force_encoding</span>(<span class="ruby-identifier">encoding</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">encoding</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">csv</span> = <span class="ruby-identifier">new</span>(<span class="ruby-identifier">str</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>) <span class="ruby-comment"># wrap</span>
  <span class="ruby-keyword">yield</span> <span class="ruby-identifier">csv</span>         <span class="ruby-comment"># yield for appending</span>
  <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">string</span>        <span class="ruby-comment"># return final String</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-generate_line" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          generate_line(ary)
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          generate_line(ary, **options)
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns the String created by generating CSV from <code>ary</code> using the specified <code>options</code>.</p>

<p>Argument <code>ary</code> must be an Array.</p>

<p>Special options:</p>
<ul><li>
<p>Option <code>:row_sep</code> defaults to <code>&quot;\n&quot;&gt; on Ruby 3.0 or later and &lt;tt&gt;$INPUT_RECORD_SEPARATOR</code> (<code>$/</code>) otherwise.:</p>

<pre class="ruby"><span class="ruby-identifier">$INPUT_RECORD_SEPARATOR</span> <span class="ruby-comment"># =&gt; &quot;\n&quot;</span>
</pre>
</li><li>
<p>This method accepts an additional option, <code>:encoding</code>, which sets the base Encoding for the output. This method will try to guess your Encoding from the first non-<code>nil</code> field in <code>row</code>, if possible, but you may need to use this parameter as a backup plan.</p>
</li></ul>

<p>For other <code>options</code>, see <a href="#class-CSV-label-Options+for+Generating">Options for Generating</a>.</p>
<hr>

<p>Returns the String generated from an Array:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_line</span>([<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-string">&#39;0&#39;</span>]) <span class="ruby-comment"># =&gt; &quot;foo,0\n&quot;</span>
</pre>
<hr>

<p>Raises an exception if <code>ary</code> is not an Array:</p>

<pre class="ruby"><span class="ruby-comment"># Raises NoMethodError (undefined method `find&#39; for :foo:Symbol)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_line</span>(<span class="ruby-value">:foo</span>)
</pre>

                              <div class="method-source-code" id="generate_line-source">
            <pre><span class="ruby-comment"># File csv.rb, line 1445</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">generate_line</span>(<span class="ruby-identifier">row</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">options</span> = {<span class="ruby-value">row_sep:</span> <span class="ruby-constant">InputRecordSeparator</span>.<span class="ruby-identifier">value</span>}.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">str</span> = <span class="ruby-string">+&quot;&quot;</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:encoding</span>]
    <span class="ruby-identifier">str</span>.<span class="ruby-identifier">force_encoding</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:encoding</span>])
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">fallback_encoding</span> = <span class="ruby-keyword">nil</span>
    <span class="ruby-identifier">output_encoding</span> = <span class="ruby-keyword">nil</span>
    <span class="ruby-identifier">row</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">field</span><span class="ruby-operator">|</span>
      <span class="ruby-keyword">next</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">String</span>)
      <span class="ruby-identifier">fallback_encoding</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">encoding</span>
      <span class="ruby-keyword">next</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">ascii_only?</span>
      <span class="ruby-identifier">output_encoding</span> = <span class="ruby-identifier">field</span>.<span class="ruby-identifier">encoding</span>
      <span class="ruby-keyword">break</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">output_encoding</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">fallback_encoding</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">output_encoding</span>
      <span class="ruby-identifier">str</span>.<span class="ruby-identifier">force_encoding</span>(<span class="ruby-identifier">output_encoding</span>)
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  (<span class="ruby-identifier">new</span>(<span class="ruby-identifier">str</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>) <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">row</span>).<span class="ruby-identifier">string</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-generate_lines" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          generate_lines(rows)
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          generate_lines(rows, **options)
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns the String created by generating CSV from using the specified <code>options</code>.</p>

<p>Argument <code>rows</code> must be an Array of row. <a href="CSV/Row.html"><code>Row</code></a> is Array of String or CSV::Row.</p>

<p>Special options:</p>
<ul><li>
<p>Option <code>:row_sep</code> defaults to <code>&quot;\n&quot;</code> on Ruby 3.0 or later and <code>$INPUT_RECORD_SEPARATOR</code> (<code>$/</code>) otherwise.:</p>

<pre class="ruby"><span class="ruby-identifier">$INPUT_RECORD_SEPARATOR</span> <span class="ruby-comment"># =&gt; &quot;\n&quot;</span>
</pre>
</li><li>
<p>This method accepts an additional option, <code>:encoding</code>, which sets the base Encoding for the output. This method will try to guess your Encoding from the first non-<code>nil</code> field in <code>row</code>, if possible, but you may need to use this parameter as a backup plan.</p>
</li></ul>

<p>For other <code>options</code>, see <a href="#class-CSV-label-Options+for+Generating">Options for Generating</a>.</p>
<hr>

<p>Returns the String generated from an</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_lines</span>([[<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-string">&#39;0&#39;</span>], [<span class="ruby-string">&#39;bar&#39;</span>, <span class="ruby-string">&#39;1&#39;</span>], [<span class="ruby-string">&#39;baz&#39;</span>, <span class="ruby-string">&#39;2&#39;</span>]]) <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
</pre>
<hr>

<p>Raises an exception</p>

<pre class="ruby"><span class="ruby-comment"># Raises NoMethodError (undefined method `each&#39; for :foo:Symbol)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate_lines</span>(<span class="ruby-value">:foo</span>)
</pre>

                              <div class="method-source-code" id="generate_lines-source">
            <pre><span class="ruby-comment"># File csv.rb, line 1500</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">generate_lines</span>(<span class="ruby-identifier">rows</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-operator">**</span><span class="ruby-identifier">options</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">rows</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">row</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-instance" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          instance(string, **options)
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          instance(io = $stdout, **options)
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          instance(string, **options) {|csv| ... }
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          instance(io = $stdout, **options) {|csv| ... }
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Creates or retrieves cached CSV objects. For arguments and options, see <a href="CSV.html#method-c-new"><code>CSV.new</code></a>.</p>

<p>This API is not Ractor-safe.</p>
<hr>

<p>With no block given, returns a CSV object.</p>

<p>The first call to <code>instance</code> creates and caches a CSV object:</p>

<pre class="ruby"><span class="ruby-identifier">s0</span> = <span class="ruby-string">&#39;s0&#39;</span>
<span class="ruby-identifier">csv0</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">instance</span>(<span class="ruby-identifier">s0</span>)
<span class="ruby-identifier">csv0</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; CSV</span>
</pre>

<p>Subsequent calls to <code>instance</code> with that <em>same</em> <code>string</code> or <code>io</code> retrieve that same cached object:</p>

<pre class="ruby"><span class="ruby-identifier">csv1</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">instance</span>(<span class="ruby-identifier">s0</span>)
<span class="ruby-identifier">csv1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; CSV</span>
<span class="ruby-identifier">csv1</span>.<span class="ruby-identifier">equal?</span>(<span class="ruby-identifier">csv0</span>) <span class="ruby-comment"># =&gt; true # Same CSV object</span>
</pre>

<p>A subsequent call to <code>instance</code> with a <em>different</em> <code>string</code> or <code>io</code> creates and caches a <em>different</em> CSV object.</p>

<pre class="ruby"><span class="ruby-identifier">s1</span> = <span class="ruby-string">&#39;s1&#39;</span>
<span class="ruby-identifier">csv2</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">instance</span>(<span class="ruby-identifier">s1</span>)
<span class="ruby-identifier">csv2</span>.<span class="ruby-identifier">equal?</span>(<span class="ruby-identifier">csv0</span>) <span class="ruby-comment"># =&gt; false # Different CSV object</span>
</pre>

<p>All the cached objects remains available:</p>

<pre class="ruby"><span class="ruby-identifier">csv3</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">instance</span>(<span class="ruby-identifier">s0</span>)
<span class="ruby-identifier">csv3</span>.<span class="ruby-identifier">equal?</span>(<span class="ruby-identifier">csv0</span>) <span class="ruby-comment"># true # Same CSV object</span>
<span class="ruby-identifier">csv4</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">instance</span>(<span class="ruby-identifier">s1</span>)
<span class="ruby-identifier">csv4</span>.<span class="ruby-identifier">equal?</span>(<span class="ruby-identifier">csv2</span>) <span class="ruby-comment"># true # Same CSV object</span>
</pre>
<hr>

<p>When a block is given, calls the block with the created or retrieved CSV object; returns the block’s return value:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">instance</span>(<span class="ruby-identifier">s0</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span> <span class="ruby-value">:foo</span> } <span class="ruby-comment"># =&gt; :foo</span>
</pre>

                              <div class="method-source-code" id="instance-source">
            <pre><span class="ruby-comment"># File csv.rb, line 1005</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">instance</span>(<span class="ruby-identifier">data</span> = <span class="ruby-identifier">$stdout</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
  <span class="ruby-comment"># create a _signature_ for this method call, data object and options</span>
  <span class="ruby-identifier">sig</span> = [<span class="ruby-identifier">data</span>.<span class="ruby-identifier">object_id</span>] <span class="ruby-operator">+</span>
        <span class="ruby-identifier">options</span>.<span class="ruby-identifier">values_at</span>(<span class="ruby-operator">*</span><span class="ruby-constant">DEFAULT_OPTIONS</span>.<span class="ruby-identifier">keys</span>)

  <span class="ruby-comment"># fetch or create the instance for this signature</span>
  <span class="ruby-identifier">@@instances</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">Hash</span>.<span class="ruby-identifier">new</span>
  <span class="ruby-identifier">instance</span> = (<span class="ruby-identifier">@@instances</span>[<span class="ruby-identifier">sig</span>] <span class="ruby-operator">||=</span> <span class="ruby-identifier">new</span>(<span class="ruby-identifier">data</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>))

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
    <span class="ruby-keyword">yield</span> <span class="ruby-identifier">instance</span>  <span class="ruby-comment"># run block, if given, returning result</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">instance</span>        <span class="ruby-comment"># or return the instance</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-new" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          new(string)
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          new(io)
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          new(string, **options)
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          new(io, **options)
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns the new CSV object created using <code>string</code> or <code>io</code> and the specified <code>options</code>.</p>
<ul><li>
<p>Argument <code>string</code> should be a String object; it will be put into a new StringIO object positioned at the beginning.</p>
</li><li>
<p>Argument <code>io</code> should be an IO object that is:</p>
<ul><li>
<p>Open for reading; on return, the IO object will be closed.</p>
</li><li>
<p>Positioned at the beginning. To position at the end, for appending, use method <a href="CSV.html#method-c-generate"><code>CSV.generate</code></a>. For any other positioning, pass a preset StringIO object instead.</p>
</li></ul>
</li><li>
<p>Argument <code>options</code>: See:</p>
<ul><li>
<p><a href="#class-CSV-label-Options+for+Parsing">Options for Parsing</a></p>
</li><li>
<p><a href="#class-CSV-label-Options+for+Generating">Options for Generating</a></p>
</li></ul>

<p>For performance reasons, the options cannot be overridden in a CSV object, so those specified here will endure.</p>
</li></ul>

<p>In addition to the CSV instance methods, several IO methods are delegated. See <a href="#class-CSV-label-Delegated+Methods">Delegated Methods</a>.</p>
<hr>

<p>Create a CSV object from a String object:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;foo,0&#39;</span>)
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; #&lt;CSV io_type:StringIO encoding:UTF-8 lineno:0 col_sep:&quot;,&quot; row_sep:&quot;\n&quot; quote_char:&quot;\&quot;&quot;&gt;</span>
</pre>

<p>Create a CSV object from a File object:</p>

<pre class="ruby"><span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-string">&#39;t.csv&#39;</span>, <span class="ruby-string">&#39;foo,0&#39;</span>)
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">&#39;t.csv&#39;</span>))
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; #&lt;CSV io_type:File io_path:&quot;t.csv&quot; encoding:UTF-8 lineno:0 col_sep:&quot;,&quot; row_sep:&quot;\n&quot; quote_char:&quot;\&quot;&quot;&gt;</span>
</pre>
<hr>

<p>Raises an exception if the argument is <code>nil</code>:</p>

<pre class="ruby"><span class="ruby-comment"># Raises ArgumentError (Cannot parse nil as CSV):</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword">nil</span>)
</pre>

                              <div class="method-source-code" id="new-source">
            <pre><span class="ruby-comment"># File csv.rb, line 1904</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">data</span>,
               <span class="ruby-value">col_sep:</span> <span class="ruby-string">&quot;,&quot;</span>,
               <span class="ruby-value">row_sep:</span> <span class="ruby-value">:auto</span>,
               <span class="ruby-value">quote_char:</span> <span class="ruby-string">&#39;&quot;&#39;</span>,
               <span class="ruby-value">field_size_limit:</span> <span class="ruby-keyword">nil</span>,
               <span class="ruby-value">max_field_size:</span> <span class="ruby-keyword">nil</span>,
               <span class="ruby-value">converters:</span> <span class="ruby-keyword">nil</span>,
               <span class="ruby-value">unconverted_fields:</span> <span class="ruby-keyword">nil</span>,
               <span class="ruby-value">headers:</span> <span class="ruby-keyword">false</span>,
               <span class="ruby-value">return_headers:</span> <span class="ruby-keyword">false</span>,
               <span class="ruby-value">write_headers:</span> <span class="ruby-keyword">nil</span>,
               <span class="ruby-value">header_converters:</span> <span class="ruby-keyword">nil</span>,
               <span class="ruby-value">skip_blanks:</span> <span class="ruby-keyword">false</span>,
               <span class="ruby-value">force_quotes:</span> <span class="ruby-keyword">false</span>,
               <span class="ruby-value">skip_lines:</span> <span class="ruby-keyword">nil</span>,
               <span class="ruby-value">liberal_parsing:</span> <span class="ruby-keyword">false</span>,
               <span class="ruby-value">internal_encoding:</span> <span class="ruby-keyword">nil</span>,
               <span class="ruby-value">external_encoding:</span> <span class="ruby-keyword">nil</span>,
               <span class="ruby-value">encoding:</span> <span class="ruby-keyword">nil</span>,
               <span class="ruby-value">nil_value:</span> <span class="ruby-keyword">nil</span>,
               <span class="ruby-value">empty_value:</span> <span class="ruby-string">&quot;&quot;</span>,
               <span class="ruby-value">strip:</span> <span class="ruby-keyword">false</span>,
               <span class="ruby-value">quote_empty:</span> <span class="ruby-keyword">true</span>,
               <span class="ruby-value">write_converters:</span> <span class="ruby-keyword">nil</span>,
               <span class="ruby-value">write_nil_value:</span> <span class="ruby-keyword">nil</span>,
               <span class="ruby-value">write_empty_value:</span> <span class="ruby-string">&quot;&quot;</span>)
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;Cannot parse nil as CSV&quot;</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">nil?</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">String</span>)
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">encoding</span>
      <span class="ruby-keyword">if</span> <span class="ruby-identifier">encoding</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">String</span>)
        <span class="ruby-identifier">data_external_encoding</span>, <span class="ruby-identifier">data_internal_encoding</span> = <span class="ruby-identifier">encoding</span>.<span class="ruby-identifier">split</span>(<span class="ruby-string">&quot;:&quot;</span>, <span class="ruby-value">2</span>)
        <span class="ruby-keyword">if</span> <span class="ruby-identifier">data_internal_encoding</span>
          <span class="ruby-identifier">data</span> = <span class="ruby-identifier">data</span>.<span class="ruby-identifier">encode</span>(<span class="ruby-identifier">data_internal_encoding</span>, <span class="ruby-identifier">data_external_encoding</span>)
        <span class="ruby-keyword">else</span>
          <span class="ruby-identifier">data</span> = <span class="ruby-identifier">data</span>.<span class="ruby-identifier">dup</span>.<span class="ruby-identifier">force_encoding</span>(<span class="ruby-identifier">data_external_encoding</span>)
        <span class="ruby-keyword">end</span>
      <span class="ruby-keyword">else</span>
        <span class="ruby-identifier">data</span> = <span class="ruby-identifier">data</span>.<span class="ruby-identifier">dup</span>.<span class="ruby-identifier">force_encoding</span>(<span class="ruby-identifier">encoding</span>)
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-ivar">@io</span> = <span class="ruby-constant">StringIO</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">data</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-ivar">@io</span> = <span class="ruby-identifier">data</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-ivar">@encoding</span> = <span class="ruby-identifier">determine_encoding</span>(<span class="ruby-identifier">encoding</span>, <span class="ruby-identifier">internal_encoding</span>)

  <span class="ruby-ivar">@base_fields_converter_options</span> = {
    <span class="ruby-value">nil_value:</span> <span class="ruby-identifier">nil_value</span>,
    <span class="ruby-value">empty_value:</span> <span class="ruby-identifier">empty_value</span>,
  }
  <span class="ruby-ivar">@write_fields_converter_options</span> = {
    <span class="ruby-value">nil_value:</span> <span class="ruby-identifier">write_nil_value</span>,
    <span class="ruby-value">empty_value:</span> <span class="ruby-identifier">write_empty_value</span>,
  }
  <span class="ruby-ivar">@initial_converters</span> = <span class="ruby-identifier">converters</span>
  <span class="ruby-ivar">@initial_header_converters</span> = <span class="ruby-identifier">header_converters</span>
  <span class="ruby-ivar">@initial_write_converters</span> = <span class="ruby-identifier">write_converters</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">max_field_size</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">field_size_limit</span>
    <span class="ruby-identifier">max_field_size</span> = <span class="ruby-identifier">field_size_limit</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-ivar">@parser_options</span> = {
    <span class="ruby-value">column_separator:</span> <span class="ruby-identifier">col_sep</span>,
    <span class="ruby-value">row_separator:</span> <span class="ruby-identifier">row_sep</span>,
    <span class="ruby-value">quote_character:</span> <span class="ruby-identifier">quote_char</span>,
    <span class="ruby-value">max_field_size:</span> <span class="ruby-identifier">max_field_size</span>,
    <span class="ruby-value">unconverted_fields:</span> <span class="ruby-identifier">unconverted_fields</span>,
    <span class="ruby-value">headers:</span> <span class="ruby-identifier">headers</span>,
    <span class="ruby-value">return_headers:</span> <span class="ruby-identifier">return_headers</span>,
    <span class="ruby-value">skip_blanks:</span> <span class="ruby-identifier">skip_blanks</span>,
    <span class="ruby-value">skip_lines:</span> <span class="ruby-identifier">skip_lines</span>,
    <span class="ruby-value">liberal_parsing:</span> <span class="ruby-identifier">liberal_parsing</span>,
    <span class="ruby-value">encoding:</span> <span class="ruby-ivar">@encoding</span>,
    <span class="ruby-value">nil_value:</span> <span class="ruby-identifier">nil_value</span>,
    <span class="ruby-value">empty_value:</span> <span class="ruby-identifier">empty_value</span>,
    <span class="ruby-value">strip:</span> <span class="ruby-identifier">strip</span>,
  }
  <span class="ruby-ivar">@parser</span> = <span class="ruby-keyword">nil</span>
  <span class="ruby-ivar">@parser_enumerator</span> = <span class="ruby-keyword">nil</span>
  <span class="ruby-ivar">@eof_error</span> = <span class="ruby-keyword">nil</span>

  <span class="ruby-ivar">@writer_options</span> = {
    <span class="ruby-value">encoding:</span> <span class="ruby-ivar">@encoding</span>,
    <span class="ruby-value">force_encoding:</span> (<span class="ruby-keyword">not</span> <span class="ruby-identifier">encoding</span>.<span class="ruby-identifier">nil?</span>),
    <span class="ruby-value">force_quotes:</span> <span class="ruby-identifier">force_quotes</span>,
    <span class="ruby-value">headers:</span> <span class="ruby-identifier">headers</span>,
    <span class="ruby-value">write_headers:</span> <span class="ruby-identifier">write_headers</span>,
    <span class="ruby-value">column_separator:</span> <span class="ruby-identifier">col_sep</span>,
    <span class="ruby-value">row_separator:</span> <span class="ruby-identifier">row_sep</span>,
    <span class="ruby-value">quote_character:</span> <span class="ruby-identifier">quote_char</span>,
    <span class="ruby-value">quote_empty:</span> <span class="ruby-identifier">quote_empty</span>,
  }

  <span class="ruby-ivar">@writer</span> = <span class="ruby-keyword">nil</span>
  <span class="ruby-identifier">writer</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@writer_options</span>[<span class="ruby-value">:write_headers</span>]
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-open" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          open(file_path, mode = &quot;rb&quot;, **options ) &rarr; new_csv
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          open(io, mode = &quot;rb&quot;, **options ) &rarr; new_csv
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          open(file_path, mode = &quot;rb&quot;, **options ) { |csv| ... } &rarr; object
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          open(io, mode = &quot;rb&quot;, **options ) { |csv| ... } &rarr; object
                              </span>
                            </div>

                            <div class="method-description">
                              <p>possible options elements:</p>

<pre>keyword form:
  :invalid =&gt; nil      # raise error on invalid byte sequence (default)
  :invalid =&gt; :replace # replace invalid byte sequence
  :undef =&gt; :replace   # replace undefined conversion
  :replace =&gt; string   # replacement string (&quot;?&quot; or &quot;\uFFFD&quot; if not specified)</pre>
<ul><li>
<p>Argument <code>path</code>, if given, must be the path to a file.</p>
</li><li>
<p>Argument <code>io</code> should be an IO object that is:</p>
<ul><li>
<p>Open for reading; on return, the IO object will be closed.</p>
</li><li>
<p>Positioned at the beginning. To position at the end, for appending, use method <a href="CSV.html#method-c-generate"><code>CSV.generate</code></a>. For any other positioning, pass a preset StringIO object instead.</p>
</li></ul>
</li><li>
<p>Argument <code>mode</code>, if given, must be a File mode See <a href="IO.html#method-c-new-label-Open+Mode">Open Mode</a>.</p>
</li><li>
<p>Arguments <code>**options</code> must be keyword options. See <a href="#class-CSV-label-Options+for+Generating">Options for Generating</a>.</p>
</li><li>
<p>This method optionally accepts an additional <code>:encoding</code> option that you can use to specify the Encoding of the data read from <code>path</code> or <code>io</code>. You must provide this unless your data is in the encoding given by <code>Encoding::default_external</code>. Parsing will use this to determine how to parse the data. You may provide a second Encoding to have the data transcoded as it is read. For example,</p>

<pre>encoding: &#39;UTF-32BE:UTF-8&#39;</pre>

<p>would read <code>UTF-32BE</code> data from the file but transcode it to <code>UTF-8</code> before parsing.</p>
</li></ul>
<hr>

<p>These examples assume prior execution of:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
</pre>
<hr>

<p>With no block given, returns a new CSV object.</p>

<p>Create a CSV object using a file path:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>)
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; #&lt;CSV io_type:File io_path:&quot;t.csv&quot; encoding:UTF-8 lineno:0 col_sep:&quot;,&quot; row_sep:&quot;\n&quot; quote_char:&quot;\&quot;&quot;&gt;</span>
</pre>

<p>Create a CSV object using an open File:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>))
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; #&lt;CSV io_type:File io_path:&quot;t.csv&quot; encoding:UTF-8 lineno:0 col_sep:&quot;,&quot; row_sep:&quot;\n&quot; quote_char:&quot;\&quot;&quot;&gt;</span>
</pre>
<hr>

<p>With a block given, calls the block with the created CSV object; returns the block’s return value:</p>

<p>Using a file path:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">csv</span>}
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; #&lt;CSV io_type:File io_path:&quot;t.csv&quot; encoding:UTF-8 lineno:0 col_sep:&quot;,&quot; row_sep:&quot;\n&quot; quote_char:&quot;\&quot;&quot;&gt;</span>
</pre>

<p>Output:</p>

<pre class="ruby"><span class="ruby-comment">#&lt;CSV io_type:File io_path:&quot;t.csv&quot; encoding:UTF-8 lineno:0 col_sep:&quot;,&quot; row_sep:&quot;\n&quot; quote_char:&quot;\&quot;&quot;&gt;</span>
</pre>

<p>Using an open File:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>)) {<span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">csv</span>}
<span class="ruby-identifier">csv</span> <span class="ruby-comment"># =&gt; #&lt;CSV io_type:File io_path:&quot;t.csv&quot; encoding:UTF-8 lineno:0 col_sep:&quot;,&quot; row_sep:&quot;\n&quot; quote_char:&quot;\&quot;&quot;&gt;</span>
</pre>

<p>Output:</p>

<pre class="ruby"><span class="ruby-comment">#&lt;CSV io_type:File io_path:&quot;t.csv&quot; encoding:UTF-8 lineno:0 col_sep:&quot;,&quot; row_sep:&quot;\n&quot; quote_char:&quot;\&quot;&quot;&gt;</span>
</pre>
<hr>

<p>Raises an exception if the argument is not a String object or IO object:</p>

<pre class="ruby"><span class="ruby-comment"># Raises TypeError (no implicit conversion of Symbol into String)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-value">:foo</span>)
</pre>

                              <div class="method-source-code" id="open-source">
            <pre><span class="ruby-comment"># File csv.rb, line 1580</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">open</span>(<span class="ruby-identifier">filename</span>, <span class="ruby-identifier">mode</span>=<span class="ruby-string">&quot;r&quot;</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
  <span class="ruby-comment"># wrap a File opened with the remaining +args+ with no newline</span>
  <span class="ruby-comment"># decorator</span>
  <span class="ruby-identifier">file_opts</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">dup</span>
  <span class="ruby-keyword">unless</span> <span class="ruby-identifier">file_opts</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-value">:newline</span>)
    <span class="ruby-identifier">file_opts</span>[<span class="ruby-value">:universal_newline</span>] <span class="ruby-operator">||=</span> <span class="ruby-keyword">false</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:invalid</span>)
  <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:undef</span>)
  <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:replace</span>)
  <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete_if</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">k</span>, <span class="ruby-identifier">_</span><span class="ruby-operator">|</span> <span class="ruby-regexp">/newline\z/</span>.<span class="ruby-identifier">match?</span>(<span class="ruby-identifier">k</span>)}

  <span class="ruby-keyword">begin</span>
    <span class="ruby-identifier">f</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">filename</span>, <span class="ruby-identifier">mode</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">file_opts</span>)
  <span class="ruby-keyword">rescue</span> <span class="ruby-constant">ArgumentError</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-identifier">e</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-keyword">unless</span> <span class="ruby-regexp">/needs binmode/</span>.<span class="ruby-identifier">match?</span>(<span class="ruby-identifier">e</span>.<span class="ruby-identifier">message</span>) <span class="ruby-keyword">and</span> <span class="ruby-identifier">mode</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;r&quot;</span>
    <span class="ruby-identifier">mode</span> = <span class="ruby-string">&quot;rb&quot;</span>
    <span class="ruby-identifier">file_opts</span> = {<span class="ruby-value">encoding:</span> <span class="ruby-constant">Encoding</span>.<span class="ruby-identifier">default_external</span>}.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">file_opts</span>)
    <span class="ruby-keyword">retry</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">begin</span>
    <span class="ruby-identifier">csv</span> = <span class="ruby-identifier">new</span>(<span class="ruby-identifier">f</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
  <span class="ruby-keyword">rescue</span> <span class="ruby-constant">Exception</span>
    <span class="ruby-identifier">f</span>.<span class="ruby-identifier">close</span>
    <span class="ruby-identifier">raise</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-comment"># handle blocks like Ruby&#39;s open(), not like the CSV library</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
    <span class="ruby-keyword">begin</span>
      <span class="ruby-keyword">yield</span> <span class="ruby-identifier">csv</span>
    <span class="ruby-keyword">ensure</span>
      <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">close</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">csv</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-parse" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          parse(string) &rarr; array_of_arrays
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          parse(io) &rarr; array_of_arrays
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          parse(string, headers: ..., **options) &rarr; csv_table
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          parse(io, headers: ..., **options) &rarr; csv_table
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          parse(string, **options) {|row| ... }
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          parse(io, **options) {|row| ... }
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Parses <code>string</code> or <code>io</code> using the specified <code>options</code>.</p>
<ul><li>
<p>Argument <code>string</code> should be a String object; it will be put into a new StringIO object positioned at the beginning.</p>
</li><li>
<p>Argument <code>io</code> should be an IO object that is:</p>
<ul><li>
<p>Open for reading; on return, the IO object will be closed.</p>
</li><li>
<p>Positioned at the beginning. To position at the end, for appending, use method <a href="CSV.html#method-c-generate"><code>CSV.generate</code></a>. For any other positioning, pass a preset StringIO object instead.</p>
</li></ul>
</li><li>
<p>Argument <code>options</code>: see <a href="#class-CSV-label-Options+for+Parsing">Options for Parsing</a></p>
</li></ul>

<h6 id="method-c-parse-label-Without+Option+headers">Without Option <code>headers</code><span><a href="#method-c-parse-label-Without+Option+headers">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Without {option <code>headers</code><a href="#class-CSV-label-Option+headers">}</a> case.</p>

<p>These examples assume prior execution of:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
</pre>
<hr>

<p>With no block given, returns an Array of Arrays formed from the source.</p>

<p>Parse a String:</p>

<pre class="ruby"><span class="ruby-identifier">a_of_a</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-identifier">a_of_a</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>Parse an open File:</p>

<pre class="ruby"><span class="ruby-identifier">a_of_a</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">file</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">a_of_a</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>
<hr>

<p>With a block given, calls the block with each parsed row:</p>

<p>Parse a String:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">row</span> }
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-string">&quot;foo&quot;</span>, <span class="ruby-string">&quot;0&quot;</span>]
[<span class="ruby-string">&quot;bar&quot;</span>, <span class="ruby-string">&quot;1&quot;</span>]
[<span class="ruby-string">&quot;baz&quot;</span>, <span class="ruby-string">&quot;2&quot;</span>]
</pre>

<p>Parse an open File:</p>

<pre class="ruby"><span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">file</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">row</span> }
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-string">&quot;foo&quot;</span>, <span class="ruby-string">&quot;0&quot;</span>]
[<span class="ruby-string">&quot;bar&quot;</span>, <span class="ruby-string">&quot;1&quot;</span>]
[<span class="ruby-string">&quot;baz&quot;</span>, <span class="ruby-string">&quot;2&quot;</span>]
</pre>

<h6 id="method-c-parse-label-With+Option+headers">With Option <code>headers</code><span><a href="#method-c-parse-label-With+Option+headers">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>With {option <code>headers</code><a href="#class-CSV-label-Option+headers">}</a> case.</p>

<p>These examples assume prior execution of:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Count\nfoo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
</pre>
<hr>

<p>With no block given, returns a <a href="CSV/Table.html"><code>CSV::Table</code></a> object formed from the source.</p>

<p>Parse a String:</p>

<pre class="ruby"><span class="ruby-identifier">csv_table</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">headers:</span> [<span class="ruby-string">&#39;Name&#39;</span>, <span class="ruby-string">&#39;Count&#39;</span>])
<span class="ruby-identifier">csv_table</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Table mode:col_or_row row_count:5&gt;</span>
</pre>

<p>Parse an open File:</p>

<pre class="ruby"><span class="ruby-identifier">csv_table</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">file</span>, <span class="ruby-value">headers:</span> [<span class="ruby-string">&#39;Name&#39;</span>, <span class="ruby-string">&#39;Count&#39;</span>])
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">csv_table</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Table mode:col_or_row row_count:4&gt;</span>
</pre>
<hr>

<p>With a block given, calls the block with each parsed row, which has been formed into a <a href="CSV/Row.html"><code>CSV::Row</code></a> object:</p>

<p>Parse a String:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">headers:</span> [<span class="ruby-string">&#39;Name&#39;</span>, <span class="ruby-string">&#39;Count&#39;</span>]) {<span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">row</span> }
</pre>

<p>Output:</p>

<pre class="ruby"><span class="ruby-comment"># &lt;CSV::Row &quot;Name&quot;:&quot;foo&quot; &quot;Count&quot;:&quot;0&quot;&gt;</span>
<span class="ruby-comment"># &lt;CSV::Row &quot;Name&quot;:&quot;bar&quot; &quot;Count&quot;:&quot;1&quot;&gt;</span>
<span class="ruby-comment"># &lt;CSV::Row &quot;Name&quot;:&quot;baz&quot; &quot;Count&quot;:&quot;2&quot;&gt;</span>
</pre>

<p>Parse an open File:</p>

<pre class="ruby"><span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">file</span>, <span class="ruby-value">headers:</span> [<span class="ruby-string">&#39;Name&#39;</span>, <span class="ruby-string">&#39;Count&#39;</span>]) {<span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">row</span> }
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre class="ruby"><span class="ruby-comment"># &lt;CSV::Row &quot;Name&quot;:&quot;foo&quot; &quot;Count&quot;:&quot;0&quot;&gt;</span>
<span class="ruby-comment"># &lt;CSV::Row &quot;Name&quot;:&quot;bar&quot; &quot;Count&quot;:&quot;1&quot;&gt;</span>
<span class="ruby-comment"># &lt;CSV::Row &quot;Name&quot;:&quot;baz&quot; &quot;Count&quot;:&quot;2&quot;&gt;</span>
</pre>
<hr>

<p>Raises an exception if the argument is not a String object or IO object:</p>

<pre class="ruby"><span class="ruby-comment"># Raises NoMethodError (undefined method `close&#39; for :foo:Symbol)</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-value">:foo</span>)
</pre>

                              <div class="method-source-code" id="parse-source">
            <pre><span class="ruby-comment"># File csv.rb, line 1731</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">parse</span>(<span class="ruby-identifier">str</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-identifier">csv</span> = <span class="ruby-identifier">new</span>(<span class="ruby-identifier">str</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)

  <span class="ruby-keyword">return</span> <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">each</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>

  <span class="ruby-comment"># slurp contents, if no block is given</span>
  <span class="ruby-keyword">begin</span>
    <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span>
  <span class="ruby-keyword">ensure</span>
    <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">close</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-parse_line" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          parse_line(string) &rarr; new_array or nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          parse_line(io) &rarr; new_array or nil
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          parse_line(string, **options) &rarr; new_array or nil
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          parse_line(io, **options) &rarr; new_array or nil
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          parse_line(string, headers: true, **options) &rarr; csv_row or nil
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          parse_line(io, headers: true, **options) &rarr; csv_row or nil
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns the data created by parsing the first line of <code>string</code> or <code>io</code> using the specified <code>options</code>.</p>
<ul><li>
<p>Argument <code>string</code> should be a String object; it will be put into a new StringIO object positioned at the beginning.</p>
</li><li>
<p>Argument <code>io</code> should be an IO object that is:</p>
<ul><li>
<p>Open for reading; on return, the IO object will be closed.</p>
</li><li>
<p>Positioned at the beginning. To position at the end, for appending, use method <a href="CSV.html#method-c-generate"><code>CSV.generate</code></a>. For any other positioning, pass a preset StringIO object instead.</p>
</li></ul>
</li><li>
<p>Argument <code>options</code>: see <a href="#class-CSV-label-Options+for+Parsing">Options for Parsing</a></p>
</li></ul>

<h6 id="method-c-parse_line-label-Without+Option+headers">Without Option <code>headers</code><span><a href="#method-c-parse_line-label-Without+Option+headers">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Without option <code>headers</code>, returns the first row as a new Array.</p>

<p>These examples assume prior execution of:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
</pre>

<p>Parse the first line from a String object:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">string</span>) <span class="ruby-comment"># =&gt; [&quot;foo&quot;, &quot;0&quot;]</span>
</pre>

<p>Parse the first line from a File object:</p>

<pre class="ruby"><span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">file</span>) <span class="ruby-comment"># =&gt; [&quot;foo&quot;, &quot;0&quot;]</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; [&quot;foo&quot;, &quot;0&quot;]</span>
</pre>

<p>Returns <code>nil</code> if the argument is an empty String:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-string">&#39;&#39;</span>) <span class="ruby-comment"># =&gt; nil</span>
</pre>

<h6 id="method-c-parse_line-label-With+Option+headers">With Option <code>headers</code><span><a href="#method-c-parse_line-label-With+Option+headers">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>With {option <code>headers</code><a href="#class-CSV-label-Option+headers">}</a>, returns the first row as a <a href="CSV/Row.html"><code>CSV::Row</code></a> object.</p>

<p>These examples assume prior execution of:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Count\nfoo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
</pre>

<p>Parse the first line from a String object:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># =&gt; #&lt;CSV::Row &quot;Name&quot;:&quot;foo&quot; &quot;Count&quot;:&quot;0&quot;&gt;</span>
</pre>

<p>Parse the first line from a File object:</p>

<pre class="ruby"><span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-identifier">file</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Row &quot;Name&quot;:&quot;foo&quot; &quot;Count&quot;:&quot;0&quot;&gt;</span>
</pre>
<hr>

<p>Raises an exception if the argument is <code>nil</code>:</p>

<pre class="ruby"><span class="ruby-comment"># Raises ArgumentError (Cannot parse nil as CSV):</span>
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">parse_line</span>(<span class="ruby-keyword">nil</span>)
</pre>

                              <div class="method-source-code" id="parse_line-source">
            <pre><span class="ruby-comment"># File csv.rb, line 1804</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">parse_line</span>(<span class="ruby-identifier">line</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">new</span>(<span class="ruby-identifier">line</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>).<span class="ruby-identifier">each</span>.<span class="ruby-identifier">first</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-read" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          read(source, **options) &rarr; array_of_arrays
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          read(source, headers: true, **options) &rarr; csv_table
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Opens the given <code>source</code> with the given <code>options</code> (see <a href="CSV.html#method-c-open"><code>CSV.open</code></a>), reads the source (see <a href="CSV.html#method-i-read"><code>CSV#read</code></a>), and returns the result, which will be either an Array of Arrays or a <a href="CSV/Table.html"><code>CSV::Table</code></a>.</p>

<p>Without headers:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">path</span>) <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>With headers:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">path</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># =&gt; #&lt;CSV::Table mode:col_or_row row_count:4&gt;</span>
</pre>

                              <div class="method-source-code" id="read-source">
            <pre><span class="ruby-comment"># File csv.rb, line 1828</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">read</span>(<span class="ruby-identifier">path</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span> <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span> }
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-readlines" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          readlines(source, **options)
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Alias for <a href="CSV.html#method-c-read"><code>CSV.read</code></a>.</p>

                              <div class="method-source-code" id="readlines-source">
            <pre><span class="ruby-comment"># File csv.rb, line 1836</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">readlines</span>(<span class="ruby-identifier">path</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">read</span>(<span class="ruby-identifier">path</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-table" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          table(source, **options)
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Calls <a href="CSV.html#method-c-read"><code>CSV.read</code></a> with <code>source</code>, <code>options</code>, and certain default options:</p>
<ul><li>
<p><code>headers</code>: <code>true</code></p>
</li><li>
<p><code>converters</code>: <code>:numeric</code></p>
</li><li>
<p><code>header_converters</code>: <code>:symbol</code></p>
</li></ul>

<p>Returns a <a href="CSV/Table.html"><code>CSV::Table</code></a> object.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">table</span>(<span class="ruby-identifier">path</span>) <span class="ruby-comment"># =&gt; #&lt;CSV::Table mode:col_or_row row_count:4&gt;</span>
</pre>

                              <div class="method-source-code" id="table-source">
            <pre><span class="ruby-comment"># File csv.rb, line 1855</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">table</span>(<span class="ruby-identifier">path</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">default_options</span> = {
    <span class="ruby-value">headers:</span>           <span class="ruby-keyword">true</span>,
    <span class="ruby-value">converters:</span>        <span class="ruby-value">:numeric</span>,
    <span class="ruby-value">header_converters:</span> <span class="ruby-value">:symbol</span>,
  }
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">default_options</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">read</span>(<span class="ruby-identifier">path</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-3C-3C" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          csv &lt;&lt; row &rarr; self
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Appends a row to <code>self</code>.</p>
<ul><li>
<p>Argument <code>row</code> must be an Array object or a <a href="CSV/Row.html"><code>CSV::Row</code></a> object.</p>
</li><li>
<p>The output stream must be open for writing.</p>
</li></ul>
<hr>

<p>Append Arrays:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;bar&#39;</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;baz&#39;</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
</pre>

<p>Append CSV::Rows:</p>

<pre class="ruby"><span class="ruby-identifier">headers</span> = []
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">Row</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">headers</span>, [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-value">0</span>])
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">Row</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">headers</span>, [<span class="ruby-string">&#39;bar&#39;</span>, <span class="ruby-value">1</span>])
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">Row</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">headers</span>, [<span class="ruby-string">&#39;baz&#39;</span>, <span class="ruby-value">2</span>])
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
</pre>

<p>Headers in <a href="CSV/Row.html"><code>CSV::Row</code></a> objects are not appended:</p>

<pre class="ruby"><span class="ruby-identifier">headers</span> = [<span class="ruby-string">&#39;Name&#39;</span>, <span class="ruby-string">&#39;Count&#39;</span>]
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">Row</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">headers</span>, [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-value">0</span>])
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">Row</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">headers</span>, [<span class="ruby-string">&#39;bar&#39;</span>, <span class="ruby-value">1</span>])
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">CSV</span><span class="ruby-operator">::</span><span class="ruby-constant">Row</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">headers</span>, [<span class="ruby-string">&#39;baz&#39;</span>, <span class="ruby-value">2</span>])
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; &quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
</pre>
<hr>

<p>Raises an exception if <code>row</code> is not an Array or CSV::Row:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-comment"># Raises NoMethodError (undefined method `collect&#39; for :foo:Symbol)</span>
  <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-value">:foo</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Raises an exception if the output stream is not opened for writing:</p>

<pre class="ruby"><span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-string">&#39;&#39;</span>)
<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">file</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
    <span class="ruby-comment"># Raises IOError (not opened for writing)</span>
    <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-value">0</span>]
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

                              <div class="method-source-code" id="3C-3C-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2371</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">&lt;&lt;</span>(<span class="ruby-identifier">row</span>)
  <span class="ruby-identifier">writer</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">row</span>
  <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="CSV.html#method-i-add_row">add_row</a>, <a href="CSV.html#method-i-puts">puts</a>
                            </div>

                          </div>

                  <div id="method-i-add_row" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">add_row</span><span
                                class="method-args">(row)</span>
                            </div>

                            <div class="method-description">
                              

                            </div>


                            <div class="aliases">
                              Alias for: <a href="CSV.html#method-i-3C-3C">&lt;&lt;</a>
                            </div>
                          </div>

                  <div id="method-i-binmode-3F" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">binmode?</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="binmode-3F-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2260</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">binmode?</span>
  <span class="ruby-keyword">if</span> <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:binmode?</span>)
    <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">binmode?</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-keyword">false</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-col_sep" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          col_sep &rarr; string
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the encoded column separator; used for parsing and writing; see {Option <code>col_sep</code><a href="#class-CSV-label-Option+col_sep">}</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">col_sep</span> <span class="ruby-comment"># =&gt; &quot;,&quot;</span>
</pre>

                              <div class="method-source-code" id="col_sep-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2008</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">col_sep</span>
  <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">column_separator</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-convert" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          convert(converter_name) &rarr; array_of_procs
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          convert {|field, field_info| ... } &rarr; array_of_procs
                              </span>
                            </div>

                            <div class="method-description">
                              <ul><li>
<p>With no block, installs a field converter (a Proc).</p>
</li><li>
<p>With a block, defines and installs a custom field converter.</p>
</li><li>
<p>Returns the Array of installed field converters.</p>
</li><li>
<p>Argument <code>converter_name</code>, if given, should be the name of an existing field converter.</p>
</li></ul>

<p>See <a href="#class-CSV-label-Field+Converters">Field Converters</a>.</p>
<hr>

<p>With no block, installs a field converter:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">convert</span>(<span class="ruby-value">:integer</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">convert</span>(<span class="ruby-value">:float</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">convert</span>(<span class="ruby-value">:date</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">converters</span> <span class="ruby-comment"># =&gt; [:integer, :float, :date]</span>
</pre>
<hr>

<p>The block, if given, is called for each field:</p>
<ul><li>
<p>Argument <code>field</code> is the field value.</p>
</li><li>
<p>Argument <code>field_info</code> is a <a href="CSV.html#FieldInfo"><code>CSV::FieldInfo</code></a> object containing details about the field.</p>
</li></ul>

<p>The examples here assume the prior execution of:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
</pre>

<p>Example giving a block:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">convert</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">field</span>, <span class="ruby-identifier">field_info</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> [<span class="ruby-identifier">field</span>, <span class="ruby-identifier">field_info</span>]; <span class="ruby-identifier">field</span>.<span class="ruby-identifier">upcase</span> }
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment"># =&gt; [[&quot;FOO&quot;, &quot;0&quot;], [&quot;BAR&quot;, &quot;1&quot;], [&quot;BAZ&quot;, &quot;2&quot;]]</span>
</pre>

<p>Output:</p>

<pre>[&quot;foo&quot;, #&lt;struct CSV::FieldInfo index=0, line=1, header=nil&gt;]
[&quot;0&quot;, #&lt;struct CSV::FieldInfo index=1, line=1, header=nil&gt;]
[&quot;bar&quot;, #&lt;struct CSV::FieldInfo index=0, line=2, header=nil&gt;]
[&quot;1&quot;, #&lt;struct CSV::FieldInfo index=1, line=2, header=nil&gt;]
[&quot;baz&quot;, #&lt;struct CSV::FieldInfo index=0, line=3, header=nil&gt;]
[&quot;2&quot;, #&lt;struct CSV::FieldInfo index=1, line=3, header=nil&gt;]</pre>

<p>The block need not return a String object:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">convert</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">field</span>, <span class="ruby-identifier">field_info</span><span class="ruby-operator">|</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">to_sym</span> }
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment"># =&gt; [[:foo, :&quot;0&quot;], [:bar, :&quot;1&quot;], [:baz, :&quot;2&quot;]]</span>
</pre>

<p>If <code>converter_name</code> is given, the block is not called:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">convert</span>(<span class="ruby-value">:integer</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">field</span>, <span class="ruby-identifier">field_info</span><span class="ruby-operator">|</span> <span class="ruby-identifier">fail</span> <span class="ruby-string">&#39;Cannot happen&#39;</span> }
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, 0], [&quot;bar&quot;, 1], [&quot;baz&quot;, 2]]</span>
</pre>
<hr>

<p>Raises a parse-time exception if <code>converter_name</code> is not the name of a built-in field converter:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">convert</span>(<span class="ruby-value">:nosuch</span>) <span class="ruby-operator">=&gt;</span> [<span class="ruby-keyword">nil</span>]
<span class="ruby-comment"># Raises NoMethodError (undefined method `arity&#39; for nil:NilClass)</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span>
</pre>

                              <div class="method-source-code" id="convert-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2442</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">convert</span>(<span class="ruby-identifier">name</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">converter</span>)
  <span class="ruby-identifier">parser_fields_converter</span>.<span class="ruby-identifier">add_converter</span>(<span class="ruby-identifier">name</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">converter</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-converters" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          converters &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an Array containing field converters; see <a href="#class-CSV-label-Field+Converters">Field Converters</a>:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">converters</span> <span class="ruby-comment"># =&gt; []</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">convert</span>(<span class="ruby-value">:integer</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">converters</span> <span class="ruby-comment"># =&gt; [:integer]</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">convert</span>(<span class="ruby-identifier">proc</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">x</span>.<span class="ruby-identifier">to_s</span> })
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">converters</span>
</pre>

<p>Notes that you need to call +Ractor.make_shareable(<a href="CSV.html#Converters"><code>CSV::Converters</code></a>)+ on the main Ractor to use this method.</p>

                              <div class="method-source-code" id="converters-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2081</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">converters</span>
  <span class="ruby-identifier">parser_fields_converter</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">converter</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">name</span> = <span class="ruby-constant">Converters</span>.<span class="ruby-identifier">rassoc</span>(<span class="ruby-identifier">converter</span>)
    <span class="ruby-identifier">name</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">name</span>.<span class="ruby-identifier">first</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">converter</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-each" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          each &rarr; enumerator
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          each {|row| ...}
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Calls the block with each successive row. The data source must be opened for reading.</p>

<p>Without headers:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">row</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-string">&quot;foo&quot;</span>, <span class="ruby-string">&quot;0&quot;</span>]
[<span class="ruby-string">&quot;bar&quot;</span>, <span class="ruby-string">&quot;1&quot;</span>]
[<span class="ruby-string">&quot;baz&quot;</span>, <span class="ruby-string">&quot;2&quot;</span>]
</pre>

<p>With headers:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">row</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre>&lt;CSV::Row &quot;Name&quot;:&quot;foo&quot; &quot;Value&quot;:&quot;0&quot;&gt;
&lt;CSV::Row &quot;Name&quot;:&quot;bar&quot; &quot;Value&quot;:&quot;1&quot;&gt;
&lt;CSV::Row &quot;Name&quot;:&quot;baz&quot; &quot;Value&quot;:&quot;2&quot;&gt;</pre>
<hr>

<p>Raises an exception if the source is not opened for reading:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">close</span>
<span class="ruby-comment"># Raises IOError (not opened for reading)</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">row</span>
<span class="ruby-keyword">end</span>
</pre>

                              <div class="method-source-code" id="each-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2553</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">each</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">to_enum</span>(<span class="ruby-identifier">__method__</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">block_given?</span>
  <span class="ruby-keyword">begin</span>
    <span class="ruby-keyword">while</span> <span class="ruby-keyword">true</span>
      <span class="ruby-keyword">yield</span>(<span class="ruby-identifier">parser_enumerator</span>.<span class="ruby-identifier">next</span>)
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">rescue</span> <span class="ruby-constant">StopIteration</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-eof" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">eof</span><span
                                class="method-args">()</span>
                            </div>

                            <div class="method-description">
                              

                            </div>


                            <div class="aliases">
                              Alias for: <a href="CSV.html#method-i-eof-3F">eof?</a>
                            </div>
                          </div>

                  <div id="method-i-eof-3F" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">eof?</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="eof-3F-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2296</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">eof?</span>
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@eof_error</span>
  <span class="ruby-keyword">begin</span>
    <span class="ruby-identifier">parser_enumerator</span>.<span class="ruby-identifier">peek</span>
    <span class="ruby-keyword">false</span>
  <span class="ruby-keyword">rescue</span> <span class="ruby-constant">MalformedCSVError</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-identifier">error</span>
    <span class="ruby-ivar">@eof_error</span> = <span class="ruby-identifier">error</span>
    <span class="ruby-keyword">false</span>
  <span class="ruby-keyword">rescue</span> <span class="ruby-constant">StopIteration</span>
    <span class="ruby-keyword">true</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="CSV.html#method-i-eof">eof</a>
                            </div>

                          </div>

                  <div id="method-i-field_size_limit" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          field_size_limit &rarr; integer or nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the limit for field size; used for parsing; see {Option <code>field_size_limit</code><a href="#class-CSV-label-Option+field_size_limit">}</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">field_size_limit</span> <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>Deprecated since 3.2.3. Use <code>max_field_size</code> instead.</p>

                              <div class="method-source-code" id="field_size_limit-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2040</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">field_size_limit</span>
  <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">field_size_limit</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-flock" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">flock</span><span
                                class="method-args">(*args)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="flock-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2268</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">flock</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">NotImplementedError</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:flock</span>)
  <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">flock</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-force_quotes-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          force_quotes? &rarr; true or false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the value that determines whether all output fields are to be quoted; used for generating; see {Option <code>force_quotes</code><a href="#class-CSV-label-Option+force_quotes">}</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">force_quotes?</span> <span class="ruby-comment"># =&gt; false</span>
</pre>

                              <div class="method-source-code" id="force_quotes-3F-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2171</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">force_quotes?</span>
  <span class="ruby-ivar">@writer_options</span>[<span class="ruby-value">:force_quotes</span>]
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-gets" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">gets</span><span
                                class="method-args">()</span>
                            </div>

                            <div class="method-description">
                              

                            </div>


                            <div class="aliases">
                              Alias for: <a href="CSV.html#method-i-shift">shift</a>
                            </div>
                          </div>

                  <div id="method-i-header_convert" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">header_convert</span><span
                                class="method-args">(name = nil, &amp;converter)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>The block need not return a String object:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">header_convert</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">header</span>, <span class="ruby-identifier">field_info</span><span class="ruby-operator">|</span> <span class="ruby-identifier">header</span>.<span class="ruby-identifier">to_sym</span> }
<span class="ruby-identifier">table</span> = <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span>
<span class="ruby-identifier">table</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [:Name, :Value]</span>
</pre>

<p>If <code>converter_name</code> is given, the block is not called:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">header_convert</span>(<span class="ruby-value">:downcase</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">header</span>, <span class="ruby-identifier">field_info</span><span class="ruby-operator">|</span> <span class="ruby-identifier">fail</span> <span class="ruby-string">&#39;Cannot happen&#39;</span> }
<span class="ruby-identifier">table</span> = <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span>
<span class="ruby-identifier">table</span>.<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; [&quot;name&quot;, &quot;value&quot;]</span>
</pre>
<hr>

<p>Raises a parse-time exception if <code>converter_name</code> is not the name of a built-in field converter:</p>

<pre class="ruby"><span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">header_convert</span>(<span class="ruby-value">:nosuch</span>)
<span class="ruby-comment"># Raises NoMethodError (undefined method `arity&#39; for nil:NilClass)</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span>
</pre>

                              <div class="method-source-code" id="header_convert-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2508</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">header_convert</span>(<span class="ruby-identifier">name</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">converter</span>)
  <span class="ruby-identifier">header_fields_converter</span>.<span class="ruby-identifier">add_converter</span>(<span class="ruby-identifier">name</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">converter</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-header_converters" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          header_converters &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an Array containing header converters; used for parsing; see <a href="#class-CSV-label-Header+Converters">Header Converters</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">header_converters</span> <span class="ruby-comment"># =&gt; []</span>
</pre>

<p>Notes that you need to call +Ractor.make_shareable(<a href="CSV.html#HeaderConverters"><code>CSV::HeaderConverters</code></a>)+ on the main Ractor to use this method.</p>

                              <div class="method-source-code" id="header_converters-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2147</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">header_converters</span>
  <span class="ruby-identifier">header_fields_converter</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">converter</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">name</span> = <span class="ruby-constant">HeaderConverters</span>.<span class="ruby-identifier">rassoc</span>(<span class="ruby-identifier">converter</span>)
    <span class="ruby-identifier">name</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">name</span>.<span class="ruby-identifier">first</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">converter</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-header_row-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          header_row? &rarr; true or false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns <code>true</code> if the next row to be read is a header row; <code>false</code> otherwise.</p>

<p>Without headers:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">header_row?</span> <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>With headers:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">header_row?</span> <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Row &quot;Name&quot;:&quot;foo&quot; &quot;Value&quot;:&quot;0&quot;&gt;</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">header_row?</span> <span class="ruby-comment"># =&gt; false</span>
</pre>
<hr>

<p>Raises an exception if the source is not opened for reading:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">close</span>
<span class="ruby-comment"># Raises IOError (not opened for reading)</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">header_row?</span>
</pre>

                              <div class="method-source-code" id="header_row-3F-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2630</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">header_row?</span>
  <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">header_row?</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-headers" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          headers &rarr; object
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the value that determines whether headers are used; used for parsing; see {Option <code>headers</code><a href="#class-CSV-label-Option+headers">}</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">headers</span> <span class="ruby-comment"># =&gt; nil</span>
</pre>

                              <div class="method-source-code" id="headers-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2105</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">headers</span>
  <span class="ruby-keyword">if</span> <span class="ruby-ivar">@writer</span>
    <span class="ruby-ivar">@writer</span>.<span class="ruby-identifier">headers</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">parsed_headers</span> = <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">headers</span>
    <span class="ruby-keyword">return</span> <span class="ruby-identifier">parsed_headers</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">parsed_headers</span>
    <span class="ruby-identifier">raw_headers</span> = <span class="ruby-ivar">@parser_options</span>[<span class="ruby-value">:headers</span>]
    <span class="ruby-identifier">raw_headers</span> = <span class="ruby-keyword">nil</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">raw_headers</span> <span class="ruby-operator">==</span> <span class="ruby-keyword">false</span>
    <span class="ruby-identifier">raw_headers</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-inspect" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          inspect &rarr; string
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns a String showing certain properties of <code>self</code>:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">s</span> = <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">inspect</span>
<span class="ruby-identifier">s</span> <span class="ruby-comment"># =&gt; &quot;#&lt;CSV io_type:StringIO encoding:UTF-8 lineno:0 col_sep:\&quot;,\&quot; row_sep:\&quot;\\n\&quot; quote_char:\&quot;\\\&quot;\&quot; headers:true&gt;&quot;</span>
</pre>

                              <div class="method-source-code" id="inspect-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2689</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">inspect</span>
  <span class="ruby-identifier">str</span> = [<span class="ruby-string">&quot;#&lt;&quot;</span>, <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-string">&quot; io_type:&quot;</span>]
  <span class="ruby-comment"># show type of wrapped IO</span>
  <span class="ruby-keyword">if</span>    <span class="ruby-ivar">@io</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">$stdout</span> <span class="ruby-keyword">then</span> <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot;$stdout&quot;</span>
  <span class="ruby-keyword">elsif</span> <span class="ruby-ivar">@io</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">$stdin</span>  <span class="ruby-keyword">then</span> <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot;$stdin&quot;</span>
  <span class="ruby-keyword">elsif</span> <span class="ruby-ivar">@io</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">$stderr</span> <span class="ruby-keyword">then</span> <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot;$stderr&quot;</span>
  <span class="ruby-keyword">else</span>                      <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">to_s</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-comment"># show IO.path(), if available</span>
  <span class="ruby-keyword">if</span> <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:path</span>) <span class="ruby-keyword">and</span> (<span class="ruby-identifier">p</span> = <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">path</span>)
    <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot; io_path:&quot;</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">p</span>.<span class="ruby-identifier">inspect</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-comment"># show encoding</span>
  <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot; encoding:&quot;</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@encoding</span>.<span class="ruby-identifier">name</span>
  <span class="ruby-comment"># show other attributes</span>
  [<span class="ruby-string">&quot;lineno&quot;</span>, <span class="ruby-string">&quot;col_sep&quot;</span>, <span class="ruby-string">&quot;row_sep&quot;</span>, <span class="ruby-string">&quot;quote_char&quot;</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">attr_name</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">a</span> = <span class="ruby-identifier">__send__</span>(<span class="ruby-identifier">attr_name</span>)
      <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot; &quot;</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">attr_name</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot;:&quot;</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">inspect</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  [<span class="ruby-string">&quot;skip_blanks&quot;</span>, <span class="ruby-string">&quot;liberal_parsing&quot;</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">attr_name</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">a</span> = <span class="ruby-identifier">__send__</span>(<span class="ruby-node">&quot;#{attr_name}?&quot;</span>)
      <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot; &quot;</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">attr_name</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot;:&quot;</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">inspect</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">_headers</span> = <span class="ruby-identifier">headers</span>
  <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot; headers:&quot;</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">_headers</span>.<span class="ruby-identifier">inspect</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">_headers</span>
  <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">&quot;&gt;&quot;</span>
  <span class="ruby-keyword">begin</span>
    <span class="ruby-identifier">str</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">&#39;&#39;</span>)
  <span class="ruby-keyword">rescue</span>  <span class="ruby-comment"># any encoding error</span>
    <span class="ruby-identifier">str</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">e</span> = <span class="ruby-constant">Encoding</span><span class="ruby-operator">::</span><span class="ruby-constant">Converter</span>.<span class="ruby-identifier">asciicompat_encoding</span>(<span class="ruby-identifier">s</span>.<span class="ruby-identifier">encoding</span>)
      <span class="ruby-identifier">e</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">s</span>.<span class="ruby-identifier">encode</span>(<span class="ruby-identifier">e</span>) <span class="ruby-operator">:</span> <span class="ruby-identifier">s</span>.<span class="ruby-identifier">force_encoding</span>(<span class="ruby-string">&quot;ASCII-8BIT&quot;</span>)
    <span class="ruby-keyword">end</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">&#39;&#39;</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-ioctl" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">ioctl</span><span
                                class="method-args">(*args)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="ioctl-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2273</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">ioctl</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">NotImplementedError</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:ioctl</span>)
  <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">ioctl</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-liberal_parsing-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          liberal_parsing? &rarr; true or false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the value that determines whether illegal input is to be handled; used for parsing; see {Option <code>liberal_parsing</code><a href="#class-CSV-label-Option+liberal_parsing">}</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">liberal_parsing?</span> <span class="ruby-comment"># =&gt; false</span>
</pre>

                              <div class="method-source-code" id="liberal_parsing-3F-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2181</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">liberal_parsing?</span>
  <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">liberal_parsing?</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-line" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          line &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the line most recently read:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">p</span> [<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">lineno</span>, <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">line</span>]
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-value">1</span>, <span class="ruby-string">&quot;foo,0\n&quot;</span>]
[<span class="ruby-value">2</span>, <span class="ruby-string">&quot;bar,1\n&quot;</span>]
[<span class="ruby-value">3</span>, <span class="ruby-string">&quot;baz,2\n&quot;</span>]
</pre>

                              <div class="method-source-code" id="line-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2246</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">line</span>
  <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">line</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-lineno" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          line_no &rarr; integer
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the count of the rows parsed or generated.</p>

<p>Parsing:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
<span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">row</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">p</span> [<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">lineno</span>, <span class="ruby-identifier">row</span>]
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-value">1</span>, [<span class="ruby-string">&quot;foo&quot;</span>, <span class="ruby-string">&quot;0&quot;</span>]]
[<span class="ruby-value">2</span>, [<span class="ruby-string">&quot;bar&quot;</span>, <span class="ruby-string">&quot;1&quot;</span>]]
[<span class="ruby-value">3</span>, [<span class="ruby-string">&quot;baz&quot;</span>, <span class="ruby-string">&quot;2&quot;</span>]]
</pre>

<p>Generating:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">generate</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">csv</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">lineno</span>; <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-value">0</span>]
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">lineno</span>; <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;bar&#39;</span>, <span class="ruby-value">1</span>]
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">csv</span>.<span class="ruby-identifier">lineno</span>; <span class="ruby-identifier">csv</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-string">&#39;baz&#39;</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">end</span>
</pre>

<p>Output:</p>

<pre class="ruby"><span class="ruby-value">0</span>
<span class="ruby-value">1</span>
<span class="ruby-value">2</span>
</pre>

                              <div class="method-source-code" id="lineno-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2222</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">lineno</span>
  <span class="ruby-keyword">if</span> <span class="ruby-ivar">@writer</span>
    <span class="ruby-ivar">@writer</span>.<span class="ruby-identifier">lineno</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">lineno</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-max_field_size" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          max_field_size &rarr; integer or nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the limit for field size; used for parsing; see {Option <code>max_field_size</code><a href="#class-CSV-label-Option+max_field_size">}</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">max_field_size</span> <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>Since 3.2.3.</p>

                              <div class="method-source-code" id="max_field_size-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2052</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">max_field_size</span>
  <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">max_field_size</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-path" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">path</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="path-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2278</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">path</span>
  <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">path</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:path</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-puts" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">puts</span><span
                                class="method-args">(row)</span>
                            </div>

                            <div class="method-description">
                              

                            </div>


                            <div class="aliases">
                              Alias for: <a href="CSV.html#method-i-3C-3C">&lt;&lt;</a>
                            </div>
                          </div>

                  <div id="method-i-quote_char" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          quote_char &rarr; character
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the encoded quote character; used for parsing and writing; see {Option <code>quote_char</code><a href="#class-CSV-label-Option+quote_char">}</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">quote_char</span> <span class="ruby-comment"># =&gt; &quot;\&quot;&quot;</span>
</pre>

                              <div class="method-source-code" id="quote_char-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2028</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">quote_char</span>
  <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">quote_character</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-read" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          read &rarr; array or csv_table
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Forms the remaining rows from <code>self</code> into:</p>
<ul><li>
<p>A <a href="CSV/Table.html"><code>CSV::Table</code></a> object, if headers are in use.</p>
</li><li>
<p>An Array of Arrays, otherwise.</p>
</li></ul>

<p>The data source must be opened for reading.</p>

<p>Without headers:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment"># =&gt; [[&quot;foo&quot;, &quot;0&quot;], [&quot;bar&quot;, &quot;1&quot;], [&quot;baz&quot;, &quot;2&quot;]]</span>
</pre>

<p>With headers:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.csv&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">string</span>)
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Table mode:col_or_row row_count:4&gt;</span>
</pre>
<hr>

<p>Raises an exception if the source is not opened for reading:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">close</span>
<span class="ruby-comment"># Raises IOError (not opened for reading)</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">read</span>
</pre>

                              <div class="method-source-code" id="read-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2594</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">read</span>
  <span class="ruby-identifier">rows</span> = <span class="ruby-identifier">to_a</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">use_headers?</span>
    <span class="ruby-constant">Table</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">rows</span>, <span class="ruby-value">headers:</span> <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">headers</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">rows</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="CSV.html#method-i-readlines">readlines</a>
                            </div>

                          </div>

                  <div id="method-i-readline" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">readline</span><span
                                class="method-args">()</span>
                            </div>

                            <div class="method-description">
                              

                            </div>


                            <div class="aliases">
                              Alias for: <a href="CSV.html#method-i-shift">shift</a>
                            </div>
                          </div>

                  <div id="method-i-readlines" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">readlines</span><span
                                class="method-args">()</span>
                            </div>

                            <div class="method-description">
                              

                            </div>


                            <div class="aliases">
                              Alias for: <a href="CSV.html#method-i-read">read</a>
                            </div>
                          </div>

                  <div id="method-i-return_headers-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          return_headers? &rarr; true or false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the value that determines whether headers are to be returned; used for parsing; see {Option <code>return_headers</code><a href="#class-CSV-label-Option+return_headers">}</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">return_headers?</span> <span class="ruby-comment"># =&gt; false</span>
</pre>

                              <div class="method-source-code" id="return_headers-3F-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2123</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">return_headers?</span>
  <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">return_headers?</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-rewind" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">rewind</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Rewinds the underlying IO object and resets CSV’s lineno() counter.</p>

                              <div class="method-source-code" id="rewind-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2311</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">rewind</span>
  <span class="ruby-ivar">@parser</span> = <span class="ruby-keyword">nil</span>
  <span class="ruby-ivar">@parser_enumerator</span> = <span class="ruby-keyword">nil</span>
  <span class="ruby-ivar">@eof_error</span> = <span class="ruby-keyword">nil</span>
  <span class="ruby-ivar">@writer</span>.<span class="ruby-identifier">rewind</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@writer</span>
  <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">rewind</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-row_sep" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          row_sep &rarr; string
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the encoded row separator; used for parsing and writing; see {Option <code>row_sep</code><a href="#class-CSV-label-Option+row_sep">}</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">row_sep</span> <span class="ruby-comment"># =&gt; &quot;\n&quot;</span>
</pre>

                              <div class="method-source-code" id="row_sep-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2018</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">row_sep</span>
  <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">row_separator</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-shift" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          shift &rarr; array, csv_row, or nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the next row of data as:</p>
<ul><li>
<p>An Array if no headers are used.</p>
</li><li>
<p>A <a href="CSV/Row.html"><code>CSV::Row</code></a> object if headers are used.</p>
</li></ul>

<p>The data source must be opened for reading.</p>

<p>Without headers:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; [&quot;foo&quot;, &quot;0&quot;]</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; [&quot;bar&quot;, &quot;1&quot;]</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; [&quot;baz&quot;, &quot;2&quot;]</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>With headers:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;Name,Value\nfoo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>, <span class="ruby-value">headers:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Row &quot;Name&quot;:&quot;foo&quot; &quot;Value&quot;:&quot;0&quot;&gt;</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Row &quot;Name&quot;:&quot;bar&quot; &quot;Value&quot;:&quot;1&quot;&gt;</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; #&lt;CSV::Row &quot;Name&quot;:&quot;baz&quot; &quot;Value&quot;:&quot;2&quot;&gt;</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span> <span class="ruby-comment"># =&gt; nil</span>
</pre>
<hr>

<p>Raises an exception if the source is not opened for reading:</p>

<pre class="ruby"><span class="ruby-identifier">string</span> = <span class="ruby-string">&quot;foo,0\nbar,1\nbaz,2\n&quot;</span>
<span class="ruby-identifier">csv</span> = <span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">string</span>)
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">close</span>
<span class="ruby-comment"># Raises IOError (not opened for reading)</span>
<span class="ruby-identifier">csv</span>.<span class="ruby-identifier">shift</span>
</pre>

                              <div class="method-source-code" id="shift-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2667</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">shift</span>
  <span class="ruby-keyword">if</span> <span class="ruby-ivar">@eof_error</span>
    <span class="ruby-identifier">eof_error</span>, <span class="ruby-ivar">@eof_error</span> = <span class="ruby-ivar">@eof_error</span>, <span class="ruby-keyword">nil</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-identifier">eof_error</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">begin</span>
    <span class="ruby-identifier">parser_enumerator</span>.<span class="ruby-identifier">next</span>
  <span class="ruby-keyword">rescue</span> <span class="ruby-constant">StopIteration</span>
    <span class="ruby-keyword">nil</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="CSV.html#method-i-gets">gets</a>, <a href="CSV.html#method-i-readline">readline</a>
                            </div>

                          </div>

                  <div id="method-i-skip_blanks-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          skip_blanks? &rarr; true or false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the value that determines whether blank lines are to be ignored; used for parsing; see {Option <code>skip_blanks</code><a href="#class-CSV-label-Option+skip_blanks">}</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">skip_blanks?</span> <span class="ruby-comment"># =&gt; false</span>
</pre>

                              <div class="method-source-code" id="skip_blanks-3F-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2160</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">skip_blanks?</span>
  <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">skip_blanks?</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-skip_lines" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          skip_lines &rarr; regexp or nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the Regexp used to identify comment lines; used for parsing; see {Option <code>skip_lines</code><a href="#class-CSV-label-Option+skip_lines">}</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">skip_lines</span> <span class="ruby-comment"># =&gt; nil</span>
</pre>

                              <div class="method-source-code" id="skip_lines-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2062</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">skip_lines</span>
  <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">skip_lines</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-stat" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">stat</span><span
                                class="method-args">(*args)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="stat-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2282</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">stat</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">NotImplementedError</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:stat</span>)
  <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">stat</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-to_i" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">to_i</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="to_i-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2287</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">to_i</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">NotImplementedError</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:to_i</span>)
  <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">to_i</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-to_io" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">to_io</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="to_io-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2292</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">to_io</span>
  <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:to_io</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">to_io</span> <span class="ruby-operator">:</span> <span class="ruby-ivar">@io</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-unconverted_fields-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          unconverted_fields? &rarr; object
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the value that determines whether unconverted fields are to be available; used for parsing; see {Option <code>unconverted_fields</code><a href="#class-CSV-label-Option+unconverted_fields">}</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">unconverted_fields?</span> <span class="ruby-comment"># =&gt; nil</span>
</pre>

                              <div class="method-source-code" id="unconverted_fields-3F-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2095</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">unconverted_fields?</span>
  <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">unconverted_fields?</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-write_headers-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          write_headers? &rarr; true or false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the value that determines whether headers are to be written; used for generating; see {Option <code>write_headers</code><a href="#class-CSV-label-Option+write_headers">}</a>:</p>

<pre class="ruby"><span class="ruby-constant">CSV</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;&#39;</span>).<span class="ruby-identifier">write_headers?</span> <span class="ruby-comment"># =&gt; nil</span>
</pre>

                              <div class="method-source-code" id="write_headers-3F-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2133</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">write_headers?</span>
  <span class="ruby-ivar">@writer_options</span>[<span class="ruby-value">:write_headers</span>]
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="private-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Private Instance Methods</h3>
                </header>

                  <div id="method-i-build_fields_converter" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">build_fields_converter</span><span
                                class="method-args">(initial_converters, options)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="build_fields_converter-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2821</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">build_fields_converter</span>(<span class="ruby-identifier">initial_converters</span>, <span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">fields_converter</span> = <span class="ruby-constant">FieldsConverter</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">normalize_converters</span>(<span class="ruby-identifier">initial_converters</span>).<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">name</span>, <span class="ruby-identifier">converter</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">fields_converter</span>.<span class="ruby-identifier">add_converter</span>(<span class="ruby-identifier">name</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">converter</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">fields_converter</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-build_header_fields_converter" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">build_header_fields_converter</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="build_header_fields_converter-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2803</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">build_header_fields_converter</span>
  <span class="ruby-identifier">specific_options</span> = {
    <span class="ruby-value">builtin_converters_name:</span> <span class="ruby-value">:HeaderConverters</span>,
    <span class="ruby-value">accept_nil:</span> <span class="ruby-keyword">true</span>,
  }
  <span class="ruby-identifier">options</span> = <span class="ruby-ivar">@base_fields_converter_options</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">specific_options</span>)
  <span class="ruby-identifier">build_fields_converter</span>(<span class="ruby-ivar">@initial_header_converters</span>, <span class="ruby-identifier">options</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-build_parser_fields_converter" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">build_parser_fields_converter</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="build_parser_fields_converter-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2791</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">build_parser_fields_converter</span>
  <span class="ruby-identifier">specific_options</span> = {
    <span class="ruby-value">builtin_converters_name:</span> <span class="ruby-value">:Converters</span>,
  }
  <span class="ruby-identifier">options</span> = <span class="ruby-ivar">@base_fields_converter_options</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">specific_options</span>)
  <span class="ruby-identifier">build_fields_converter</span>(<span class="ruby-ivar">@initial_converters</span>, <span class="ruby-identifier">options</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-build_writer_fields_converter" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">build_writer_fields_converter</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="build_writer_fields_converter-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2816</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">build_writer_fields_converter</span>
  <span class="ruby-identifier">build_fields_converter</span>(<span class="ruby-ivar">@initial_write_converters</span>,
                         <span class="ruby-ivar">@write_fields_converter_options</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-convert_fields" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">convert_fields</span><span
                                class="method-args">(fields, headers = false)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Processes <code>fields</code> with <code>@converters</code>, or <code>@header_converters</code> if <code>headers</code> is passed as <code>true</code>, returning the converted field set. Any converter that changes the field into something other than a String halts the pipeline of conversion for that field. This is primarily an efficiency shortcut.</p>

                              <div class="method-source-code" id="convert_fields-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2766</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">convert_fields</span>(<span class="ruby-identifier">fields</span>, <span class="ruby-identifier">headers</span> = <span class="ruby-keyword">false</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">headers</span>
    <span class="ruby-identifier">header_fields_converter</span>.<span class="ruby-identifier">convert</span>(<span class="ruby-identifier">fields</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-value">0</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">parser_fields_converter</span>.<span class="ruby-identifier">convert</span>(<span class="ruby-identifier">fields</span>, <span class="ruby-ivar">@headers</span>, <span class="ruby-identifier">lineno</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-determine_encoding" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">determine_encoding</span><span
                                class="method-args">(encoding, internal_encoding)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="determine_encoding-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2729</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">determine_encoding</span>(<span class="ruby-identifier">encoding</span>, <span class="ruby-identifier">internal_encoding</span>)
  <span class="ruby-comment"># honor the IO encoding if we can, otherwise default to ASCII-8BIT</span>
  <span class="ruby-identifier">io_encoding</span> = <span class="ruby-identifier">raw_encoding</span>
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">io_encoding</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">io_encoding</span>

  <span class="ruby-keyword">return</span> <span class="ruby-constant">Encoding</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">internal_encoding</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">internal_encoding</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">encoding</span>
    <span class="ruby-identifier">encoding</span>, = <span class="ruby-identifier">encoding</span>.<span class="ruby-identifier">split</span>(<span class="ruby-string">&quot;:&quot;</span>, <span class="ruby-value">2</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">encoding</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">String</span>)
    <span class="ruby-keyword">return</span> <span class="ruby-constant">Encoding</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">encoding</span>)
  <span class="ruby-keyword">end</span>

  <span class="ruby-constant">Encoding</span>.<span class="ruby-identifier">default_internal</span> <span class="ruby-operator">||</span> <span class="ruby-constant">Encoding</span>.<span class="ruby-identifier">default_external</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-header_fields_converter" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">header_fields_converter</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="header_fields_converter-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2799</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">header_fields_converter</span>
  <span class="ruby-ivar">@header_fields_converter</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">build_header_fields_converter</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-normalize_converters" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">normalize_converters</span><span
                                class="method-args">(converters)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="normalize_converters-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2744</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">normalize_converters</span>(<span class="ruby-identifier">converters</span>)
  <span class="ruby-identifier">converters</span> <span class="ruby-operator">||=</span> []
  <span class="ruby-keyword">unless</span> <span class="ruby-identifier">converters</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Array</span>)
    <span class="ruby-identifier">converters</span> = [<span class="ruby-identifier">converters</span>]
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">converters</span>.<span class="ruby-identifier">collect</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">converter</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">case</span> <span class="ruby-identifier">converter</span>
    <span class="ruby-keyword">when</span> <span class="ruby-constant">Proc</span> <span class="ruby-comment"># custom code block</span>
      [<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">converter</span>]
    <span class="ruby-keyword">else</span> <span class="ruby-comment"># by name</span>
      [<span class="ruby-identifier">converter</span>, <span class="ruby-keyword">nil</span>]
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-parser" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">parser</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="parser-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2829</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">parser</span>
  <span class="ruby-ivar">@parser</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">Parser</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@io</span>, <span class="ruby-identifier">parser_options</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-parser_enumerator" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">parser_enumerator</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="parser_enumerator-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2838</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">parser_enumerator</span>
  <span class="ruby-ivar">@parser_enumerator</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">parser</span>.<span class="ruby-identifier">parse</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-parser_fields_converter" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">parser_fields_converter</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="parser_fields_converter-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2787</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">parser_fields_converter</span>
  <span class="ruby-ivar">@parser_fields_converter</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">build_parser_fields_converter</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-parser_options" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">parser_options</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="parser_options-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2833</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">parser_options</span>
  <span class="ruby-ivar">@parser_options</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-value">header_fields_converter:</span> <span class="ruby-identifier">header_fields_converter</span>,
                        <span class="ruby-value">fields_converter:</span> <span class="ruby-identifier">parser_fields_converter</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-raw_encoding" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">raw_encoding</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the encoding of the internal IO object.</p>

                              <div class="method-source-code" id="raw_encoding-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2777</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">raw_encoding</span>
  <span class="ruby-keyword">if</span> <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:internal_encoding</span>
    <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">internal_encoding</span> <span class="ruby-operator">||</span> <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">external_encoding</span>
  <span class="ruby-keyword">elsif</span> <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:encoding</span>
    <span class="ruby-ivar">@io</span>.<span class="ruby-identifier">encoding</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-keyword">nil</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-writer" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">writer</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="writer-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2842</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">writer</span>
  <span class="ruby-ivar">@writer</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">Writer</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@io</span>, <span class="ruby-identifier">writer_options</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-writer_fields_converter" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">writer_fields_converter</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="writer_fields_converter-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2812</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">writer_fields_converter</span>
  <span class="ruby-ivar">@writer_fields_converter</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">build_writer_fields_converter</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-writer_options" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">writer_options</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="writer_options-source">
            <pre><span class="ruby-comment"># File csv.rb, line 2846</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">writer_options</span>
  <span class="ruby-ivar">@writer_options</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-value">header_fields_converter:</span> <span class="ruby-identifier">header_fields_converter</span>,
                        <span class="ruby-value">fields_converter:</span> <span class="ruby-identifier">writer_fields_converter</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is provided by <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>.</p><p><a href="https://jamesbritt.bandcamp.com/">Maximum R+D</a>.  </p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

